Jump to content

[SOLVED] if rows returned equal zero


jaxdevil

Recommended Posts

Below is my code, what I want to do is have the while statement give an alternate variable definitions for $amount, $authxyz, and $cc_num if the query returns 0 rows. Basically I want it to just say N/A if there are no entries for the query. Any ideas?

 

Thanks in advance,

SK

 

<?php
$sql = "SELECT * FROM checkout WHERE bidnum=$di AND deposit_storage=1 AND type=cc";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$amount = $row['checkout'];
$authxyz = $row['ref'];
$cc_num = $row['cc_num'];
}
?>
<?php
$number = "$cc_num";
$last_four = substr($number, -4);
$first_one = substr($number,0,1);
echo $first_one;
if($first_one=='3'){$cc_type = 'American Express';}
elseif($first_one=='4'){$cc_type = 'Visa';}
elseif($first_one=='5'){$cc_type = 'MasterCard';}
elseif($first_one=='6'){$cc_type = 'Discover';}
else {$cc_type = 'Unknown';}
?>

Link to comment
https://forums.phpfreaks.com/topic/82217-solved-if-rows-returned-equal-zero/
Share on other sites

I do something similar in a program I am writing (though I use mysqli functions):

 

<?php
$databaseConnect = mysqli_connect("localhost", "username", "pass", "db")
	Or die("Unable to connect to the database.");

$query = "SELECT type_ID FROM Article_Type WHERE typeName = '$articleType'";
$result = mysqli_query($databaseConnect, $query);

if (mysqli_num_rows($result) == 0)
     echo 'NA';
else {
     echo 'Winner winner chicken dinner';
}
?>

<?php
$sql = "SELECT * FROM checkout WHERE bidnum=$di AND deposit_storage=1 AND type=cc";
$query = mysql_query($sql);
//set defaults for the values here.
$amount = "N/A";
$authxyz = "N/A";
$cc_num = "N/A";
while($row = mysql_fetch_array($query)) {
$amount = $row['checkout'];
$authxyz = $row['ref'];
$cc_num = $row['cc_num'];
}
?>
<?php
$number = "$cc_num";
$last_four = substr($number, -4);
$first_one = substr($number,0,1);
echo $first_one;
if($first_one=='3'){$cc_type = 'American Express';}
elseif($first_one=='4'){$cc_type = 'Visa';}
elseif($first_one=='5'){$cc_type = 'MasterCard';}
elseif($first_one=='6'){$cc_type = 'Discover';}
else {$cc_type = 'Unknown';}
?>

 

I don't think the while loop will ever execute if there aren't any rows, but if it does, just use myslq_num_rows to check first.

Thats PERFECT! I forgot it will not execute if it equals zero. That makes it even better. Here is what I am using now (posted below) so if a customer is a credit card customer it shows their cc data and signature line, if not a cc customer it just shows the signature and date lines (every receipt has to be signed, the cc receipts just kills two birds with one stone) so now its dynamic based on the customer. CC customers see a CC based receipt, the other payment customers just do not see the cc data. SWEET! THANKS GUYS!!

 

SK

 

<table width="800" height="50" cellspacing="0" cellpadding="0" border="1" bordercolor="black">
<?php
$sql = "SELECT * FROM checkout WHERE bidnum='$di' AND deposit_storage='1' AND type='cc'";
$query = mysql_query($sql) or die(mysql_error()); 
while($row = mysql_fetch_array($query)) {
$amount = $row['checkout'];
$authxyz = $row['ref'];
$cc_num = $row['cc_num'];
?>
<?php
$number = "$cc_num";
$last_four = substr($number, -4);
$first_one = substr($number,0,1);
if($first_one=='3'){$cc_type = 'American Express';}
elseif($first_one=='4'){$cc_type = 'Visa';}
elseif($first_one=='5'){$cc_type = 'MasterCard';}
elseif($first_one=='6'){$cc_type = 'Discover';}
else {$cc_type = 'Unknown';}
?>
<tr valign="top">
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">CARD TYPE</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">LAST 4</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">AUTH CODE</font>
</td>
<td width="200" height="10" align="left" valign="top">
<font face="Tahoma" size="2">AMOUNT</font>
</td>
</tr>
<tr valign="top">
<td width="200" height="40" align="center" valign="middle">
<font face="Tahoma" size="2"><?=$cc_type?></font>
</td>
<td width="200" height="40" align="center" valign="middle">
<font face="Tahoma" size="2"><?=$last_four?></font>
</td>
<td width="200" height="40" align="center" valign="middle">
<font face="Tahoma" size="2"><?=$authxyz?></font>
</td>
<td width="200" height="40" align="center" valign="middle">
<font face="Tahoma" size="2"><?=$amount?></font>
</td>
</tr>
<?php
}
?>
<tr valign="top">
<td width="400" height="50" align="left" valign="top" colspan="2">
<font face="Tahoma" size="2">SIGNATURE</font>
</td>
<td width="400" height="50" align="left" valign="top" colspan="2">
<font face="Tahoma" size="2">DATE</font>
</td>
</tr>
</table>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.