Jump to content

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


Go to solution Solved by Sepodati,

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>";

}}

?>

Use whatever tool that the odbc interface provides for you to check how many rows were returned.  If zero, then don't output the table startup and just echo your message and then do an exit().

 

No?

  • Solution

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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.