0

I have a problem.

When I try to select data from a table using this code:

$sql = "SELECT * FROM users";
$result = mysql_query($sql);

while($rad = mysql_fetch_array($result))
{
    echo $rad['user'];
}

It works.

When I select data from another table with basically the same code I get an error.

$sql = "SELECT * FROM order";
$result = mysql_query($sql);

while($rad = mysql_fetch_array($result))
{
    echo $rad['url'];
}

Can someone please tell me what I'm doing wrong here.

This is the error message I get when I run the second code:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

Kim Andersson
  • 251
  • 2
  • 4
  • 15

2 Answers2

1

Your result is returning FALSE , meaning there is an error in your query.

from php.net:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

It's possible you have no rights on this table.

Do this to show me the error:

$sql = "SELECT * FROM order";
$result = mysql_query($sql);
echo mysql_error(); 

PROBLEM FOUND:

After seeing your mysql error everything is clear.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1

You have to chance:

$sql = "SELECT * FROM order";

to

$sql = "SELECT * FROM `order`";

Putting order between backticks (“`”), because order is a reserved mysql word for order by operations.

Reserved words are permitted as identifiers if you quote them as described in Section 9.2, “Schema Object Names”:

From Mysql.com

Reserved words are listed at Section 9.3, “Reserved Words”.

Timmetje
  • 7,641
  • 18
  • 36
  • This is the error I get: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1" – Kim Andersson Feb 21 '13 at 20:53
  • 1
    Ah ok i see you need to put order between single quotes because order is a reserved word. – Timmetje Feb 21 '13 at 20:55
0

"with basically the same code" ...

This is most likely due to you lacking a connection. You can troubleshoot using:

$result = mysql_query($sql);
if (false === $result) {
    echo mysql_error();
}

See this thread for more troubleshooting steps.

Also, please do not use mysql_ functions. 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.

Community
  • 1
  • 1
Kermit
  • 33,827
  • 13
  • 85
  • 121