-4

I have an SQL query that works, and inside the loop I want a call to another SQL query which I expect only one result, but I get an error.

Here is the code:

$i = 0;
$query = mysql_query("SELECT * FROM `orders` ORDER BY `ID` ASC ");
while($row = mysql_fetch_array($query)) 
{
    $i++;
    $productResult = mysql_select_db("SELECT * FROM `page` WHERE 'id'=".$i."'");
}

And here is what is want to get from this code:

<?=$productResult['title'];?>

How can I fix it? I know the problem is in this line:

mysql_select_db("SELECT * FROM `page` WHERE 'id'=".$i."'");
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • 1
    Learn to use prepared statements with MySQLi or PDO. The MySQL library is deprecated – Mark Baker Mar 31 '13 at 11:50
  • also this `=$productResult['title'];?>` makes no sense if you want to print information use `echo` or `print` or if you want to manipulate use it where you want – NullPoiиteя Mar 31 '13 at 12:14
  • i am really afraid it there will be thousand of rows ... it mean thousand of queries if you are showing result like so shows question use pagination or something like that and use `limit` or anything else to control query and eliminate query from loop – NullPoiиteя Mar 31 '13 at 12:17

2 Answers2

6

You need to remove the single quotes around id - if you want to quote a table/column name in MySQL you need to use backticks. However, there's no reason to do so unless you use a name that is a reserved keyword.

You also need to replace mysql_select_db with mysql_query.

Last but not least, sending queries within a loop is a bad idea and you forgot to actually retrieve your results.

And finally, since you appear to have no PHP/MySQL knowledge at all yet, here's a suggestion that you should really follow:
Please, don'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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

try this

mysql_query("SELECT * FROM `page` WHERE `id`=".$i);

I'd recommend to use PDO or MySqli since MySql_* is deprecated.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
Alexey
  • 7,127
  • 9
  • 57
  • 94