Page 1 of 2
[SOLVED] Returning a 1 or 0
Posted: Fri Apr 01, 2005 6:07 am
by rsmarsha
I have the following code:
Code: Select all
$outcome = "UPDATE orders SET status='$newstatus' WHERE orderno='$SR'";
$outcomeq = mysql_query($outcome, $db_conn) or die("Query $outcomeq Failed".mysql_error());
$return = mysql_affected_rows($outcomeq);
echo 'test '.$return.'';
I need to return a 1 or a 0 if the update query updates something, so i tried a row count, and echo'd that. For some reason it shows nothing even though 1 row has been updated. Any ideas?
Posted: Fri Apr 01, 2005 6:27 am
by feyd
define "it shows nothing"
At a minimum, you should see 'test'. Do a var_dump() of $return.
Posted: Fri Apr 01, 2005 6:28 am
by Paddy
Looks ok to me.
mysql_affected_rows returns a zero though if the update doesn't change anything. For instance if status was "paid" and you set status to "paid" via an update then even though the update has happened mysql_affected_rows would return 0.
Posted: Fri Apr 01, 2005 6:37 am
by rsmarsha
I'm trying to get it to return a 1 if it updates something and 0 if no updates takes place. At the moment i just get "test" and nothing after that.
Posted: Fri Apr 01, 2005 12:40 pm
by feyd
an update query returns TRUE or FALSE, nothing more. You can convert that to a 1 or 0 using
intval()
Posted: Tue Apr 05, 2005 3:13 am
by rsmarsha
Still don't understand why i won't echo the row count.
intval works great, but if the query run and doesn't update something will this still show a 1?
Posted: Tue Apr 05, 2005 4:10 am
by rsmarsha
Really annoying me now, hehe. Always used a row count before, this doens't seem to work on this code for some reason.
Posted: Tue Apr 05, 2005 4:37 am
by CoderGoblin
Doesn't
need to be
I always get confused.
Posted: Tue Apr 05, 2005 5:09 am
by rsmarsha
hmm that seems to work for the update query, unless the status is not changed in which case it won't show a 1. I need it to show even if the status isn't changed.
Posted: Tue Apr 05, 2005 5:17 am
by CoderGoblin
Sorry I am confused about your requirement. If the sql is not sucessful you die (gracefully) (php stops). If not, judging from what you just said, the update is always 1 isn't it ?
mysql_affected_rows just lists the number of rows affected/that have been changed. Normally text goes something like "x Records updated in database". If no changes are made "No changes required in database".
Posted: Tue Apr 05, 2005 5:22 am
by CoderGoblin
Looking at it from a different way. If you want to find how many records could change try another select beforehand...
Code: Select all
SELECT count(status) FROM orders WHERE orderno='$SR'"e;;
This would give the number of matches for orderno='$SR';
Posted: Tue Apr 05, 2005 5:30 am
by rsmarsha
It is for an external company to update finance app status on an order. They have some kind of auto recognition script that requires me to return a 1 or a 0. mysql_Affected_rows returns a 1 if updated but a 0 if no rows are changed. I need it to return a 1 if the query runs (finds a match to the query), if it updates or not. The important thing is that the match is found to the record, and if it needs changing it's changed, if not then i still get a 1 as long as it's found.
Posted: Tue Apr 05, 2005 5:46 am
by CoderGoblin
Code: Select all
$result=mysql_query("SELECT count(status) FROM orders WHERE orderno='$SR'",$db_conn);
$qty=@mysql_result($result,0,0);
if ($qty) {
$return=1;
$outcome = "UPDATE orders SET status='$newstatus' WHERE orderno='$SR'";
$outcomeq = mysql_query($outcome, $db_conn) or die("Query $outcomeq Failed".mysql_error());
} else {
$return=0;
}
echo 'test '.$return.'';
Should do the trick (not tested).
Posted: Tue Apr 05, 2005 6:03 am
by rsmarsha
The man is a genius.
Works a treat!
The code in full is now:
Code: Select all
include 'db.php';
//credit outcome
$SR = $_GET['SR'];
$app_id = $_GET['REF'];
$auth = $_GET['Auth'];
if ($_GET['Status']=='A')
{
$newstatus = 'Finance Accepted';
}
elseif ($_GET['Status']=='D')
{
$newstatus = 'Finance Declined';
$reason = 'Finance Declined';
}
elseif ($_GET['Status']=='R')
{
$newstatus = 'Finance Referred';
}
elseif ($_GET['Status']=='S')
{
$newstatus = 'Agreement Received';
}
elseif ($_GET['Status']=='C')
{
$newstatus = 'Cancelled';
$reason = 'Finance Cancelled';
}
//queries
$result= mysql_query("SELECT count(status) FROM orders WHERE orderno='$SR'",$db_conn);
$qty=@mysql_result($result,0,0);
if ($qty) {
$return=1;
//update status
if ($_GET['Status']=='C' || $_GET['Status']=='D')
{
//insert into trash table
$ins = "INSERT INTO ordertrash SELECT * FROM orders WHERE orderno='$SR'";
$iq = mysql_query($ins, $db_conn) or die("Query $ins Failed".mysql_error());
//insert reason
$ins2 = "REPLACE INTO trashextra (number,reason) VALUES ('SR','$reason')";
$iq2 = mysql_query($ins2, $db_conn) or die("Query $ins2 Failed".mysql_error());
//remove from main orders table
$del = "DELETE FROM orders WHERE orderno = '$SR'";
$dq = mysql_query($del, $db_conn) or die('0');
}
else
{
$outcome = "UPDATE orders SET status='$newstatus' WHERE orderno='$SR'";
$outcomeq = mysql_query($outcome, $db_conn) or die("Query $outcomeq Failed".mysql_error());
}
//enter app id
$app_id = "REPLACE INTO orders_finance (orderno,app_id,auth) VALUES ('$SR','$app_id','$auth')";
$appq = mysql_query($app_id, $db_conn) or die("Query $appq Failed".mysql_error());
}
else {
$return=0;
}
echo ''.$return.'';
Btw any suggestions on that are always welcome. Always learning.

Posted: Tue Apr 05, 2005 6:13 am
by CoderGoblin
Rather than using
Code: Select all
if ($_GET['Status']=='A') {
} elseif ($_GET['Status']=='B') {
} elseif ($_GET['Status']=='C') {
} elseif ($_GET['Status']=='D') {
} elseif ($_GET['Status']=='E') {
} elseif ($_GET['Status']=='F') {
}
I would use a switch
http://de.php.net/manual/en/control-str ... switch.php. I find it neater and easier to track/add more options. The default should be catching where none of the Status options are matched. You should always fail gracefully - (Option X is not a valid option. This message has been logged.)