[SOLVED] Returning a 1 or 0

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

rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED] Returning a 1 or 0

Post 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?
Last edited by rsmarsha on Wed Apr 06, 2005 7:22 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

define "it shows nothing"

At a minimum, you should see 'test'. Do a var_dump() of $return.
Paddy
Forum Contributor
Posts: 244
Joined: Wed Jun 11, 2003 8:16 pm
Location: Hobart, Tas, Aussie
Contact:

Post 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.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

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

Post by feyd »

an update query returns TRUE or FALSE, nothing more. You can convert that to a 1 or 0 using intval()
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Doesn't

Code: Select all

mysql_affected_rows($outcomeq);
need to be

Code: Select all

mysql_affected_rows($db_conn);
:?:

I always get confused.
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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".
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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'&quote;;
This would give the number of matches for orderno='$SR';
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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).
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post 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. :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.)
Post Reply