0

I have the following query -

$query = mysql_query("SELECT * FROM test where admin = 'Yes' order by name = '$_GET[name]' DESC");

while($row = mysql_fetch_array($query))
{
   $id.=$row[id];
}

echo $id;

I want a list of id's starting from the id of the $_GET[name] however it's returning a list with a start name of the row underneath? Any idea's?

Many Thanks.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 2
    Please, provide a data set and what result you're getting/what you want. Your code is also vulnerable to SQL injection. Your API is deprecated. All around poor, poor code. – Kermit Feb 26 '13 at 01:15
  • 2
    Woah! You need to seriously check the security of your code. Not only are you using a deprecated library (`mysql_*`), but your code is seriously vulnerable to SQL injection! – BenM Feb 26 '13 at 01:15

1 Answers1

1
  1. Use an array

  2. You need to use quotes around colums names

$query = mysql_query("SELECT * FROM test where admin = 'Yes' order by name = '$_GET[name]' DESC");

$ids = array();
while($row = mysql_fetch_array($query))
{
   $ids[] =$row['id'];
}

// Your IDs are now in an array which is easier to work with
print_r($ids);

FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

You're also wide open to SQL injections

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496