Jump to content

how do I exit script if there is no sql results and echo "no Results Found"


kat35601

Recommended Posts

I need to check and see if there are any results if so run the script if no results then echo "no Results Found"

<?php
$connect = odbc_connect("removed");
if (!$connect) {
    exit("Connection Failed: " . $connect);
}

$sql      = "


SELECT distinct 
	ltrim(rtrim(SO.ompCustomerOrganizationID))as customer
	,ltrim(rtrim(left(cmoName,30))) as name
	,left(ltrim(rtrim(cmoAddressLine2)),30) as address1
	,ltrim(rtrim(cmoCity)) as city
	,ltrim(rtrim(cmoState)) as state
    ,ltrim(rtrim(cmoPostCode)) as postal
	, ltrim(rtrim(REPLACE(REPLACE(REPLACE(cmoPhoneNumber, '(', ''), ')', ''), '-', ''))) as phone
	
FROM m1_kf.dbo.SalesOrders SO
LEFT JOIN m1_kf.dbo.Organizations ON cmoOrganizationID = SO.ompCustomerOrganizationID
WHERE ompCreatedDate >='06-11-2017' and ompPaymentTermID in ('CN30','CTN30')
and UOMPSCHEDULENUMBER !=1 and ompOrderTotalBase > 1

";
	
$result = odbc_exec($connect, $sql);

if (!$result) {
    exit("Error in SQL");
} else (
	echo "<table><tr>";
	echo "<th>CustID</th>";
	echo "<th>OrderId</th>";
	echo "<th>Amount</th>";
	echo "<th>TotalAmount</th>";

while ($row = odbc_fetch_array($result)) {
   
    $cust_num   = $row['customer'];
    $name    = $row['name'];
    $city    = $row['city'];
    $state   = $row['state'];
   

	
	echo "<tr><td>$cust_num </td>";
    echo "<td>  $name </td>";
    echo "<td>   $city </td>";
    echo "<td> $state  </td></tr>";

}}

?>

Link to comment
Share on other sites

Fix your data BEFORE it goes in the database so you don't have to do all that ltrim, rtrim, replace crap on the way out.

 

As for the loop, either check that there are rows returned, as mentioned above, or use this method:

 

if($row = odbc_fetch_array($result)) {
  echo "<table><tr>";
  echo "<th>CustID</th>";
  echo "<th>OrderId</th>";
  echo "<th>Amount</th>";
  echo "<th>TotalAmount</th></tr>";
  do {
    echo "<tr><td>{$row['cust_num']}</td>";
    echo "<td>{$row['name']}</td>";
    echo "<td>{$row['city']}</td>";
    echo "<td>{$row['state']}</td></tr>";
  }while($row = odbc_fetch_array($result));
  echo '</table>';
} else {
  echo 'No results found.';
}
I don't like echo'ing HTML and you'd probably want to use htmlentities() if you're not doing any sanitizing of the data before it goes in. but that should give you a structure idea.
Link to comment
Share on other sites

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.