Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

Get rid of "NO ROWS FOUND".

Hi experts

In php I have a script which gets data out of a database and displays it in a table on a webpage, there will always be some data to display in the table, the problem I have is at the top of the table I get a message "no rows found", how do I get rid of this, the problem is I think in this piece of code:

if (!$conn)
                    {exit("Connection Failed: " . $conn);}
                    $sql="SELECT * FROM mytable";
                  
                  $rs=odbc_exec($conn,$sql);
                  if (!$rs)
                    {exit("Error in SQL");}
0
dizzycat
Asked:
dizzycat
  • 6
  • 3
1 Solution
 
hieloCommented:
>>, the problem is I think in this piece of code:
No. It's in the line(s) that follow what you posted.

>>there will always be some data to display in the table
If that is true, then you should not be seeing that message at all. So you are either do not have any data in the db OR the statement that checks the record count is not right. Post the complete code.
0
 
dizzycatAuthor Commented:
Here you go.
<?php
			$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=M:\<connection string>.mdb;Uid=;Pwd=;";
			$conn=odbc_connect($dsn,'','');
			if (!$conn)
			  {exit("Connection Failed: " . $conn);}
			  $sql="SELECT * FROM mytable";
			
			$rs=odbc_exec($conn,$sql);
			if (!$rs)
			  {exit("Error in SQL");}


echo "<table border=1><tr>";
echo "<th> Customer </th>";
echo "<th> Order</th>";
echo "</tr>";

while (odbc_fetch_row($rs))
{
  $f1=odbc_result($rs,"Customer");
  $f2=odbc_result($rs,"Order");
  echo "<tr>";
  echo "<td>$f1</td>";
  echo "<td>$f2</td>";
  
  //echo "</tr>";
 }



                        odbc_result_all($rs);
			odbc_close($conn);
		?>

Open in new window

0
 
hieloCommented:
try:
<?php
	$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=M:\<connection string>.mdb;Uid=;Pwd=;";
	$conn=odbc_connect($dsn,'','');
	if (!$conn)
	{
		exit("Connection Failed: " . $conn);
	}

	$sql="SELECT * FROM mytable";
                        
	$rs=odbc_exec($conn,$sql);
	if (!$rs)
	{
		exit("Error in SQL");
	}
	elseif(odbc_num_rows($rs) > 0)
	{
		echo "<table border='1'><tr>";
		echo "<th> Customer </th>";
		echo "<th> Order</th>";
		echo "</tr>";

		while (odbc_fetch_row($rs))
		{
			$f1=odbc_result($rs,"Customer");
			$f2=odbc_result($rs,"Order");
			echo "<tr>";
			echo "<td>$f1</td>";
			echo "<td>$f2</td>";
			echo "</tr>";
		}
	}
	//
	else
	{
		echo "<!-- No results found -->";	
	}
	
	odbc_close($conn);
?>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
hieloCommented:
By the way, your problem is because you have a while loop that advances through all the records returned by your query. Then later on you are executing
odbc_result_all($rs);

but by this time your while has already advanced to the end so odbc_result_all() thinks/sees no records. So, do not use odbc_result_all() if you are going to use the while() construct.
0
 
dizzycatAuthor Commented:
The above code does not return anything, if I put the odbc_result_all() back then my problem of the "no results found " is cured and the data is displayed but not in a table as I need it to be, any ideas how to use the odbc_result_all() but without the while loop but still have the data displayed in a table.
0
 
hieloCommented:
try:
<?php
	$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=M:\<connection string>.mdb;Uid=;Pwd=;";
	$conn=odbc_connect($dsn,'','');
	if (!$conn)
	{
		exit("Connection Failed: " . $conn);
	}

	$sql="SELECT [Customer], [Order] FROM mytable";
                        
	$rs=odbc_exec($conn,$sql);
	if (!$rs)
	{
		exit("Error in SQL");
	}
	elseif(odbc_num_rows($rs) > 0)
	{
		echo "<table border='1'><tr><th> Customer </th><th> Order</th></tr>";

		while (odbc_fetch_row($rs))
		{
			$f1=odbc_result($rs,1);
			$f2=odbc_result($rs,2);
			echo "<tr><td>{$f1}</td><td>{$f2}</td></tr>";
		}
		echo "</table>";
	}
	//
	else
	{
		echo "No results found!";	
	}
	odbc_free_result($rs);
	odbc_close($conn);
?>

Open in new window

0
 
hieloCommented:
>>The above code does not return anything,
Hmm, I wonder if that is  related to your driver:
http://www.php.net/manual/en/function.odbc-result-all.php#4640

It's also stated in the NOTES of odbc_num_rows()
http://us.php.net/manual/en/function.odbc-num-rows.php
if you are certain that you will always have data try:



<?php
$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=M:\<connection string>.mdb;Uid=;Pwd=;";
$conn=odbc_connect($dsn,'','');
if (!$conn)
{
	exit("Connection Failed: " . $conn);
}

$sql="SELECT [Customer], [Order] FROM mytable";
                        
$rs=odbc_exec($conn,$sql);
if (!$rs)
{
	exit("Error in SQL");
}
if( odbc_fetch_row($rs) )
{
	echo "<table border='1'><tr><th> Customer </th><th> Order</th></tr>";
	do{

		$f1=odbc_result($rs,1);
		$f2=odbc_result($rs,2);
		echo "<tr><td>{$f1}</td><td>{$f2}</td></tr>";
	}while (odbc_fetch_row($rs));
	echo "</table>";
}
else
{
	echo "No results found!";       
}
odbc_free_result($rs);
odbc_close($conn);
?>


On another note, I can't find anything that states whether odbc_result_alll() includes the field names in the first row  as headers or not? Or does it include JUST the data?

Open in new window

0
 
hieloCommented:
odbc_result_all() does include the fieldNames as headers, so try:
<?php
                        $dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=M:\<connection string>.mdb;Uid=;Pwd=;";
                        $conn=odbc_connect($dsn,'','');
                        if (!$conn)
                          {exit("Connection Failed: " . $conn);}
                          $sql="SELECT [Customer],[Order] FROM mytable";
                        
                        $rs=odbc_exec($conn,$sql);
                        if (!$rs)
                          {
					 	exit("Error in SQL");
					}


                        odbc_result_all($rs,"border='1'");
                        odbc_close($conn);
                ?>

Open in new window

0
 
dizzycatAuthor Commented:
Fantastic! thanks for your help heilo.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now