0

I just submitted my work and my course leader has asked me to use PHP functions to query my database. How can I do this? Is it possible to add just "function getRooms" before my queries, and return the desired result. I need thorough briefing on functions with MySQL.

For example: How will I add a function to this code:

<?php
    // By Kelvin
    include ("player.php");
    $result = mysql_query("SELECT * FROM phplogin WHERE username = '$username'");
    $row = mysql_fetch_assoc($result);
    $medipack = $row['medipack'];
    if ($_POST['object'] == "Use Medipack") {
        if ($medipack != 0) {
            mysql_query("UPDATE phplogin SET score = score + 50, health = health + 50, medipack = medipack - 1
            WHERE username = '$username'");
            echo ("<P>Medipack Used!</P>");
            $RoomNumber =5;
        }
        else {
            echo ("<P>You're all out of medipacks!</P>");
        }
    }
?>
5
  • First of all you should ask him why he is asking you that. Commented Mar 7, 2011 at 15:27
  • could you edit your question and add some of the code you are using? This would help with your question Commented Mar 7, 2011 at 15:27
  • @Phill Pafford.. I have added an example. How can I ad a function to that? Commented Mar 7, 2011 at 16:16
  • 1
    Why are you developing a First Person Shooter in PHP? Commented Mar 7, 2011 at 17:06
  • Wasn't my idea. Was my team members.... I opted for Java Commented Mar 7, 2011 at 17:24

3 Answers 3

1

If he's looking for a level of abstraction between the rest of your code and the code accessing the database, wrapping the database logic in functions should be on the right path:

function getRooms($link){
    $sql = "SELECT * FROM rooms";
    $data = array();
    if($set = mysql_query($sql)){
        while($row = mysql_fetch_assoc($set)){
            $data[] = $row;
        }
    }
    return empty($data)
        ? null
        : $data;
}

$db_connection = mysql_connect('host', 'user', 'pass');
mysql_select_db('name', $db_connection);
$rooms = getRooms($db_connection);

Wrapping these functions into a class, and accessing them by methods is another step, as the instantiated object can manage its own connection, etc., but that may be a step further than necessary. Other functions (based on what I can guess) might be:

function getRoomById($link, $id){ }
function getRoomsByName($link, Array $names){ }
Sign up to request clarification or add additional context in comments.

1 Comment

@Kelvin; Sorry, don't do homework outright; teach to fish and all that. What you need to do is extract your database logic into manageable functions. In this case, perhaps checkItem() to check the type of item picked up, and changeHp() to update the player HP. The example I've provided should serve as a good starting point.
0

I would imagine your course leader is referring to the PHP's native MySQL functions - http://php.net/manual/en/book.mysql.php

2 Comments

No, I think he is talking about user-defined functions. Is it possible to add just "function getRooms".
@Michiel Pater - having seen the newly added code example, I agree - please disregard my answer.
0

You can wrap your queries inside PHP functions. For example, like this:

function &getRooms(){
    $query="SELECT id,name FROM rooms";
    $result=mysql_query($query);

    $ar_out=array();
    while ($row = mysql_fetch_assoc($result)) {
        $ar_out[]=$row;
    }
    return $ar_out;
}

This said, you really should find a tutorial or a book on how to use MySQL and PHP together. There are many things to know about code organization and performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.