mysql_fetch_row(): supplied argument is not a valid MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaylin
Forum Commoner
Posts: 68
Joined: Fri Nov 18, 2005 4:44 am

mysql_fetch_row(): supplied argument is not a valid MySQL

Post by jaylin »

Sami | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


i got this error and i really have no idea to solve that problem. here my code:

Code: Select all

include "mysql.php";
			$mydbcon = new db_sql();
			$mydbcon->connect();
			$sql = "SELECT max(id) FROM order";
			$result = mysql_query($sql);
			if (isset($result))
			{
				$maxid = mysql_fetch_row($result);
				if ($maxid == false)
					$maxid = 1;
				else
					$maxid = $maxid + 1;
			}
			else
				$maxid = 1;
regards,


Sami | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

From the code I would think that the query itself is sporting an error or the connect to the database is not working but I think the object would handle that error.

BTW I don't like the if construct without {} . Bad readability even though you can do it like this.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

order is a reserved word in mysql. You shouldn't be using it as a table name but since you are quote it like this...

Code: Select all

$sql = "SELECT max(id) FROM `order`";
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You would have indentified this issue yourself if you'd used the mysql_error() function in a die() statement like so (this is good practice):

Code: Select all

include "mysql.php";
         $mydbcon = new db_sql();
         $mydbcon->connect();
         $sql = "SELECT max(id) FROM order";
         $result = mysql_query($sql) or die(mysql_error());
         if (isset($result))
         {
            $maxid = mysql_fetch_row($result);
            if ($maxid == false)
               $maxid = 1;
            else
               $maxid = $maxid + 1;
         }
         else
            $maxid = 1;
You might want to add those to your database connection class too ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

d11wtq wrote:You would have indentified this issue yourself if you'd used the mysql_error() function in a die() statement like so (this is good practice):

Code: Select all

include "mysql.php";
         $mydbcon = new db_sql();
         $mydbcon->connect();
         $sql = "SELECT max(id) FROM order";
         $result = mysql_query($sql) or die(mysql_error());
         if (isset($result))
         {
            $maxid = mysql_fetch_row($result);
            if ($maxid == false)
               $maxid = 1;
            else
               $maxid = $maxid + 1;
         }
         else
            $maxid = 1;
You might want to add those to your database connection class too ;)
Just wanted to add that this is only good practice whilst developing, when you "go live" it is a bad thing to leave the "die(mysql_error())" in as it will give any user who receives an error invaluable information about your data schema and application.
Post Reply