-2

I'm sure this is a real newbie question. I know the code works because when I tested it from without the function and it brings back the row id "75". I stick it in a function and nothing is brought back. Can anyone advise?

function gameID(){
        $gameIDsql = mysql_query("SELECT id FROM game WHERE player1='$player1' ORDER BY id DESC LIMIT 1");
        $row = mysql_fetch_assoc($gameIDsql);
        echo $row['id'];
        }

gameID();
iYugShell
  • 67
  • 2
  • 11
  • What is `print_r($row)`? – Cole Tobin Jul 25 '13 at 19:08
  • An easy way to debug this next time: Check row 75. Notice that the field `player1` is an empty string. Go back and see that `$player1` is an empty string. Notice that `$player1` is not defined in this scope (not a part of this function and not introduced as a global. – BLaZuRE Jul 25 '13 at 19:16

5 Answers5

2

There's no variable $player1 in your function. You need to change the function to take an argument:

function gameID($player1){
        $gameIDsql = mysql_query("SELECT id FROM game WHERE player1='$player1' ORDER BY id DESC LIMIT 1");
        $row = mysql_fetch_assoc($gameIDsql);
        echo $row['id'];
        }

and call it as:

gameID($player1);

You should enable error reporting, then you would have gotten an error message saying that the variable is undefined.

Barmar
  • 741,623
  • 53
  • 500
  • 612
1

It's because inside your function, you are not defining $player1 so that variable is out of scope. Your function should look more like this:

function gameID($player1){
    $gameIDsql = mysql_query("SELECT id FROM game WHERE player1='$player1' ORDER BY id DESC LIMIT 1");
    $row = mysql_fetch_assoc($gameIDsql);
    echo $row['id'];
}

That's the basic issue with your code. Other issues include:

Community
  • 1
  • 1
WWW
  • 9,734
  • 1
  • 29
  • 33
0

$player1 is not defined inside the scope of the function you need to send it as an argument for that function such as:

function gameID($player1){
        $gameIDsql = mysql_query("SELECT id FROM game WHERE player1='$player1' ORDER BY id DESC LIMIT 1");
        $row = mysql_fetch_assoc($gameIDsql);
        echo $row['id'];
        }

echo gameID();
0

You should set $player1 value, passing it as an argument for example:

function gameID($player1){
    $gameIDsql = mysql_query("SELECT id FROM game'
        . ' WHERE player1='$player1' ORDER BY id DESC LIMIT 1");
    $row = mysql_fetch_assoc($gameIDsql);
    echo $row['id'];
}

gameID('davey');
u_mulder
  • 54,101
  • 5
  • 48
  • 64
0

There are a few things you can do to fix this and prevent it from happening. Barmar is right, you need to pass the variable $player1 into your function otherwise it will be out of scope.

What's most likely happening is your query is returning an error but you're not catching it so when you try to echo $row['id'] its just empty.

While developing i always add: error_reporting(E_ALL); ini_set("display_errors", "on");

to the top of my php files to see all error and warnings.

More importantly is to catch errors, you can do something like this to ensure that your query is working properly:

function gameID($player1){
    $gameIDsql = mysql_query("SELECT id FROM game WHERE player1='$player1' ORDER BY id DESC LIMIT 1");

if(!$gameIDsql)
throw new Exception=("Unable to get id for player: " . $player1 . " query returned an error: " . mysql_error());
    $row = mysql_fetch_assoc($gameIDsql);
    echo $row['id'];
    }

Generally you it's just good practice to assume that if something can fail that it will so its best to include code that will handle the errors.

Jake
  • 513
  • 5
  • 16