Jump to content

If no data display error?


SEVIZ

Recommended Posts

Here is my simple code.  It is a simple query to search a row for data. 

<?php
$con = mysql_connect("--------","-----------","------------");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("----------", $con);

$id=$_POST['id'];
$result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'");

echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>";
while($row = mysql_fetch_array($result))
  {


echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
echo $row['num'];
echo "</td><td>";
echo $row['sup'];
echo "</td></tr>";
}
echo "</table>";
?>
<br /><br />
<form action="techdbid.php" method="post">
Tech: <input type="text" name="id"><br />
<input type="Submit">
</form>


 

What can I do so that if there is no results for the query it display an error message?  "No ID found".

 

Thank you.

Link to comment
https://forums.phpfreaks.com/topic/157753-if-no-data-display-error/
Share on other sites

Thats my problem.  I must be doing something wrong because the function returns no answer.

 

<?php
$con = mysql_connect("-------","---------","---------------");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("------", $con);

$id=$_POST['id'];
$result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'");
$num_rows = mysql_num_rows($result);
echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>";

while($row = mysql_fetch_array($result))
  {


echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
echo $row['num'];
echo "</td><td>";
echo $row['sup'];
echo "</td></tr>";
}
echo "</table>";


echo $numrows;
echo "Rows";
?>
<br /><br />
<form action="techdbid.php" method="post">
Tech: <input type="text" name="id"><br>
<input type="Submit">
</form>

 

The page goes through and there is no data there.  Its just "Rows".  With no number.

Ok heres what I got.  I can now see the num rows result but the if statement does not work.  If less than 0 it still shows blank.

 

<?php
$con = mysql_connect("-----","--------","----------");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("-------------", $con);

$id=$_POST['id'];
$result = mysql_query("SELECT * FROM sprint WHERE id LIKE '%$id%'");
$num_rows = mysql_num_rows($result);
echo "<table border='0' CELLPADDING=5 STYLE='font-size:13px'>";
echo "<tr> <td><H3>Tech ID</h3></td> <td><H3>Name</H3></td> <td><H3>Phone Number</H3></td><td><H3>Team</H3></td></tr>";

while($row = mysql_fetch_array($result))
  {

if ($num_rows > 0){ 
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['name'];
echo "</td><td>";
echo $row['num'];
echo "</td><td>";
echo $row['sup'];
echo "</td></tr>";
}else{
echo "<tr colspan=4><td>NO DATA</td></tr>";
}
}
echo "</table>";


echo $num_rows;
echo " Rows";
?>
<br /><br />
<form action="techdbid.php" method="post">
Tech: <input type="text" name="id"><br>
<input type="Submit">
</form>


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.