LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-16-2008, 03:18 AM   #1
meo_beo
Member
 
Registered: Mar 2008
Posts: 33

Rep: Reputation: 15
PHP problem


Hi guys, I'm trying to display all the records in a table, say the 'order' table, of my MySql database on a webpage using PHP. Here is the source code:


<?php
include('conn.php');
$sql = "select * from order";
$result = mysql_query($sql,$link);
?>

<body>
<center><h4>Details of each order</h4></center>
<div align="center">
<table border="1">
<tr>
<td>Order ID</td>
<td>Order Date</td>
<td>Customer ID</td>
</tr>
<?php
$total_row = mysql_num_rows($result);
if ($total_row > 0){
$i = 0;
while ($row = mysql_fetch_array($result)){
$i +=1;
?>
<tr>
<td><?= $row[0]?></td>
<td><?= $row[1]?></td>
<td><?= $row[2]?></td>
</tr>
<?php
}

}
else{
echo "Error!";
}
?>
</table>

The problem is, it always produces such an error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in
It confuses me because if I change the sql statement to "select * from order_detail", which means I only change the table name, then it works just fine. So I believe the problem is with the "order" table itself. Any suggestions?
 
Old 05-16-2008, 03:26 AM   #2
datopdog
Member
 
Registered: Feb 2008
Location: JHB South Africa
Distribution: Centos, Kubuntu, Cross LFS, OpenSolaris
Posts: 806

Rep: Reputation: 41
It means your query failed, you need to test $result first before trying to use it.
 
Old 05-16-2008, 04:56 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
To add to that, your problem can already be in conn.php; was mysql_connect successfull? Did you use mysql_select_db to select a database and was it successfull?

Further mysql_error can be of assistance to determine the error. See http://www.php.net/mysql_error

Always test the return values of functions !!
 
Old 05-16-2008, 07:06 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897Reputation: 5897
Your problem is due to order being a MySQL reserved or keyword. Try using quotes like:

$sql = "select * from `order`";
or
$sql = "select * from \"order\"";

Or do not use keywords for table names.

http://dev.mysql.com/doc/refman/5.0/...ved-words.html

BTW use code tags in the future.

Last edited by michaelk; 05-16-2008 at 08:08 AM.
 
Old 05-18-2008, 06:31 AM   #5
meo_beo
Member
 
Registered: Mar 2008
Posts: 33

Original Poster
Rep: Reputation: 15
Thank you, michaelk, I think that's what the problem is.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Urgent PHP problem with "undefined function: domxml_open_mem()"! Recomplie php? Oskare100 Linux - Server 0 12-27-2006 12:28 PM
php comment problem // and /* on Fedora Core 5 and PHP 5.1.4 shakeel_125 Linux - Server 4 09-29-2006 04:42 AM
php comment problem // and /* on Fedora Core 5 and PHP 5.1.4 shakeel_125 Fedora 4 09-29-2006 02:14 AM
New to PHP and Postgressql. Is this a PHP problem or a sql problem. Please help sendas4 Linux - General 2 11-05-2004 01:54 PM
Apache php index.php problem neurotic Linux - Software 3 11-18-2003 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration