Page 1 of 1

Update row in MySQL query

Posted: Thu Aug 05, 2004 10:19 pm
by dwfait
Hi. This piece of code does not seem to be working (ive run tests and $id is right)

Code: Select all

$result=mysql_query("UPDATE sspp SET nname='$nname' AND email='$email' WHERE id='$id'");
if (result=="TRUE") {
echo "Successfully changed account with ID:$id. <a href="index.php?action=cpanel">Click here to continue.</a>";
} else {
echo "Update did not complete succesfully. <a href="index.php?action=cpanel">Click here to continue.</a>";
}
It always comes up with Update did not complete succesfully. Can anyone help me?

Posted: Thu Aug 05, 2004 10:22 pm
by Joe

Code: Select all

$query = "UPDATE sspp SET nname='$nname' AND email='$email' WHERE id='$id'");
$result=mysql_query($query) or die(mysql_error());

if (mysql_num_rows($result))
{
echo "Successfully changed account with ID:$id. <a href="index.php?action=cpanel">Click here to continue.</a>";
 exit;
} 
else 
{
 echo "Update did not complete succesfully. <a href="index.php?action=cpanel">Click here to continue.</a>";
 exit;
}

Posted: Thu Aug 05, 2004 10:24 pm
by feyd
you may want [php_man]mysql_affected_rows[/php_man]() more than num_rows()

Posted: Thu Aug 05, 2004 10:27 pm
by Joe
Well both should work just the same, not sure about efficiency though! :D

Posted: Thu Aug 05, 2004 10:37 pm
by feyd
[php_man]mysql_affected_rows[/php_man] wrote:mysql_affected_rows() returns the number of rows affected by the last INSERT, UPDATE or DELETE query associated with link_identifier. If the link identifier isn't specified, the last link opened by mysql_connect() is assumed.
[php_man]mysql_num_rows[/php_man] wrote:mysql_num_rows() returns the number of rows in a result set. This command is only valid for SELECT statements. To retrieve the number of rows affected by a INSERT, UPDATE or DELETE query, use mysql_affected_rows().

Posted: Thu Aug 05, 2004 11:04 pm
by ol4pr0
besides that

Code: Select all

if (result=="TRUE") { 
#should of been
if ($result=="TRUE") { // you forgot the $ infront of result

Posted: Thu Aug 05, 2004 11:07 pm
by feyd
and it won't return a string containing true..

Posted: Thu Aug 05, 2004 11:19 pm
by dwfait
what will it return? and how can i get the value of its return then?

Posted: Thu Aug 05, 2004 11:28 pm
by feyd
Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query() returns a resource identifier or FALSE if the query was not executed correctly. For other type of SQL statements, mysql_query() returns TRUE on success and FALSE on error. A non-FALSE return value means that the query was legal and could be executed by the server. It does not indicate anything about the number of rows affected or returned. It is perfectly possible for a query to succeed but affect no rows or return no rows.
It'll return the value true.. not the string true.

Posted: Thu Aug 05, 2004 11:49 pm
by dwfait
so, it would be ($result=TRUE) ?

Posted: Fri Aug 06, 2004 12:01 am
by feyd
== yes.

Posted: Fri Aug 06, 2004 2:56 am
by LiquidPro
Or you could just go with...

Code: Select all

if($result) {
   // true
} else {
   // false
}

Posted: Fri Aug 06, 2004 9:23 am
by pickle
save yourself some memory space and just do

Code: Select all

$query = "blah blah";
if(mysql_query($query))
{
   //Yay!!!
}
else
{
   //Nay
}