Update row in MySQL query

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
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Update row in MySQL query

Post 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?
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post 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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may want [php_man]mysql_affected_rows[/php_man]() more than num_rows()
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Well both should work just the same, not sure about efficiency though! :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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().
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

besides that

Code: Select all

if (result=="TRUE") { 
#should of been
if ($result=="TRUE") { // you forgot the $ infront of result
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

and it won't return a string containing true..
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

what will it return? and how can i get the value of its return then?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
dwfait
Forum Contributor
Posts: 113
Joined: Sun Aug 01, 2004 10:36 pm

Post by dwfait »

so, it would be ($result=TRUE) ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

== yes.
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Or you could just go with...

Code: Select all

if($result) {
   // true
} else {
   // false
}
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

save yourself some memory space and just do

Code: Select all

$query = "blah blah";
if(mysql_query($query))
{
   //Yay!!!
}
else
{
   //Nay
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply