Page 1 of 1

mysql_fetch_row(): supplied argument is not a valid MySQL

Posted: Mon Dec 05, 2005 11:32 pm
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]

Posted: Tue Dec 06, 2005 12:35 am
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.

Posted: Tue Dec 06, 2005 4:04 am
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`";

Posted: Tue Dec 06, 2005 4:58 am
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 ;)

Posted: Tue Dec 06, 2005 7:06 am
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.