UPDATE?
Moderator: General Moderators
UPDATE?
ok i understand that update returns the number of rows updated...
but
if i attempt to update a row that _doesnt_ exist, it still returns 1!
why
but
if i attempt to update a row that _doesnt_ exist, it still returns 1!
why
i was trying to do error checking...
am trying to avoid having to script a check as i want to try keep off the sql server as much as poss
still dont understand how you can update a non-existant row
am trying to avoid having to script a check as i want to try keep off the sql server as much as poss
Code: Select all
if($planet = $HTTP_POST_VARSї'planet']){
$title = $HTTP_POST_VARSї'title'];
$result = mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'");
if($result == 1)
echo 'Planet name successfully changed';
else
echo 'error changing planet name';Hey C,
Well, according to the manual, mysql_query() returns true or false. Therefore, you could do something like this.
Or, to shorten it up by a line,...
Just double check with the manual to see what functions return.
Cheers,
BDKR
Well, according to the manual, mysql_query() returns true or false. Therefore, you could do something like this.
Code: Select all
if($planet = $HTTP_POST_VARSї'planet'])
{
$title = $HTTP_POST_VARSї'title'];
$result = mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'");
if($result == true)
{ echo 'Planet name successfully changed'; }
elseif($result == false)
{ echo 'error changing planet name'; }
}Code: Select all
if($planet = $HTTP_POST_VARSї'planet'])
{
$title = $HTTP_POST_VARSї'title'];
if(mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'")==true)
{ echo 'Planet name successfully changed'; }
else
{ echo 'error changing planet name'; }
}Cheers,
BDKR
Hey Coco,
From the manual...
BDKR
Sorry about the line above. It's true, but on certain types of queries. The way that sentence is written above, it seems as though it's correct for all queries.Well, according to the manual, mysql_query() returns true or false. Therefore, you could do something like this.
From the manual...
Cheers,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.
BDKR
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
try using this function:
http://php.net/mysql_affected_rows
http://php.net/mysql_affected_rows
- mydimension
- Moderator
- Posts: 531
- Joined: Tue Apr 23, 2002 6:00 pm
- Location: Lowell, MA USA
- Contact:
to me this ought to work and makes perfect sense. if it dosen't work there maybe more information that we don't know yet
Code: Select all
if($planet = $HTTP_POST_VARSї'planet']){
$title = $HTTP_POST_VARSї'title'];
$result = mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'");
$num = mysql_affected_rows($result);
if($num > 0){
echo 'Planet name successfully changed'; }
elseif($num =< 0){
echo 'error changing planet name';
}
}- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
$result = mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'") or die("foo!");HOW (well it didnt work
so it takes me back to my origional question
why does update return a value of 1 when it attempts to change a non-existant row?
sure i can code around it but that means an extra call to the sql server and i really wanna try keep that to a minimum
Something to think about. If you type in something like "SELECT COUNT(*) FROM testTable", the answer you're going to get is only 1 / ONE row long, inspite of how many rows are in the table.
Similarly, if your query is an update query, mysql is going to return something to tell you that it worked or flailed. If there was a failure due to something like a table or field that doesn't exist, that false return is going to be 1 / ONE row long. In other words, that 1 row is to tell you that the query bombed!
The manual is your friend.
Cheers,
BDKR
well i changed my script as was suggested and it didnt work...
i understand that mysql_query returns true or false on an update, BUT it only returns false IF the query is INVALID...
so affected rows must be used instead:
no matter what i put as values for title and planet it always affects 1 row
always
*sighs* guess i may aswell code in a select query to check if the name was changed
i understand that mysql_query returns true or false on an update, BUT it only returns false IF the query is INVALID...
so affected rows must be used instead:
Code: Select all
if($planet = $HTTP_POST_VARSї'planet']){
$title = $HTTP_POST_VARSї'title'];
mysql_query("UPDATE Planet SET title = '$title' WHERE PID = '$planet'");
if(mysql_affected_rows() == 1)
echo 'Planet name successfully changed';
else
echo 'error changing planet name';
}//end ifalways
*sighs* guess i may aswell code in a select query to check if the name was changed