0

I am having a problem with MySQL and PHP. I'm trying to create something that gets values from a database, encode it in JSON and then print it. I have that down, but there's something that is keeping from one certain row in the database from displaying. It always returns NULL except for the id value I set. Here's my code, am I doing something wrong?

$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
    $playerInfo = array('id' => $playerarray['id'], 'name' => $playerarray['name'], 'server' => $playerarray['server']);
    echo(json_encode($playerInfo));
}

If you want to take a look at it, it's hosted here. The funny thing is, this page uses the exact same code, but doesn't return null. Any ideas?

Edit:

Here's what is in $playerInfo (when I use geekygamer14)

array (size=3)
  'id' => null
  'name' => null
  'server' => null

It seems that whatever rows that have verified set to 1 (integer), it gives NULL.

5
  • What you have in $playerInfo? var_dump it and post it in the question Commented Dec 31, 2013 at 7:15
  • 2
    what is $playerarray?, it should be $playerInfo = array('id' => $record['id'], 'name' => $record['name'], 'server' => $record['server']); Commented Dec 31, 2013 at 7:18
  • Even the id is null in the results, I think your query doesn't matched any row in the table players Commented Dec 31, 2013 at 7:18
  • So where does $playerarray come from and what has the while-loop to do with it? Commented Dec 31, 2013 at 7:24
  • Found the problem... $playerarray was equal to mysql_fetch_array($srv). Whoops. Commented Dec 31, 2013 at 7:26

2 Answers 2

1

You're using variable $record in your loop, though this doesn't show in your generated array. Instead you're using a variable named $playerarray. I assume the code should be:

while ($record = mysql_fetch_array($srv)) {
    $playerInfo = array('id' => $record['id'], 'name' => $record['name'], 'server' => $record['server']);
    echo(json_encode($playerInfo));
}

Note: Consider using an alternative to mysql-functions, for example mysqli. Mysql-functions are deprecated.

Sign up to request clarification or add additional context in comments.

1 Comment

Oh wow. I really am that stupid. I guess I should get some sleep before I completely break everything.
0

Change this

$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
    $playerInfo = array('id' => $playerarray['id'], 'name' => $playerarray['name'], 'server' => $playerarray['server']);
    echo(json_encode($playerInfo));
}

to this

$srv = mysql_query("SELECT * FROM `players` WHERE `name` LIKE '%" . mysql_real_escape_string($_GET['q']) . "%'");
while ($record = mysql_fetch_array($srv)) {
    $playerInfo = array('id' => $record['id'], 'name' => $record['name'], 'server' => $record['server']);
    echo(json_encode($playerInfo));
}

Your variables name $playerarray['id'] is wrong this is write $record['id']. Your data is in $record because of loop so you should use $record instead of $playerarray.

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.