0

below is a code to display table rows fetched from DB when the Sql Query returnes result.

<?php
if(isset(['submit']))
{
$key=$_POST['txt_srchKey'];
}



if($key!="")
{
$all_entries=mysql_query("SELECT * FROM tbl_book WHERE bookAuthor='$key'|| bookName='$key'||bookCode='$key' "); 
?>  
<table border=1 align="left">
<tr>
<td><b>Book Code</b></td><td><b>Book Name</b></td><td><b>Edition</b></td><td><b>Author</b></td>
</tr>
<?php
while($row=mysql_fetch_array($all_entries))
{ ?>

<tr>
<td><?php echo $row['bookCode'] ?></td>
<td><?php echo $row['bookName'] ?></td>

<td><?php echo $row['bookEdition'] ?></td>
<td><?php echo $row['bookAuthor'] ?></td>
</tr>
<?php
}   
?>              
</table>

I am getting the result in the table when the condition (bookAuthor='$key'|| bookName='$key'||bookCode='$key')is true.but if there is no result it only showing the table headins eg:Book Code, Book Name etc. But instead of this I need to display 'No result found'.How can I do this by editing my code?

1
  • Firstly, this if(isset(['submit'])) is invalid. It should read as if(isset($_POST['submit'])) Commented Jul 26, 2014 at 4:41

4 Answers 4

1

Without immediately getting into the problems with your code, you can use mysql_num_rows.

<?php
if(isset(['submit']))
{
    $key=$_POST['txt_srchKey'];
}
if($key!="")
{
    $all_entries=mysql_query("SELECT * FROM tbl_book WHERE bookAuthor='$key'|| bookName='$key'||bookCode='$key' "); 
    if(mysql_num_rows($all_entries) > 0)
    {
        ?>  
        <table border=1 align="left">
        <tr>
            <td><b>Book Code</b></td><td><b>Book Name</b></td><td><b>Edition</b></td><td><b>Author</b></td>
        </tr>
        <?php
        while($row=mysql_fetch_array($all_entries))
        {
        ?>
            <tr>
                <td><?php echo $row['bookCode'] ?></td>
                <td><?php echo $row['bookName'] ?></td>
                <td><?php echo $row['bookEdition'] ?></td>
                <td><?php echo $row['bookAuthor'] ?></td>
            </tr>
        <?php
        }   
        ?>              
        </table>
        <?php
    }
    else
    {
        ?><p>No result found</p><?php
    }
}

There are a few things about this code that I should draw attention to:


Issue 1

This line is invalid:

if(isset(['submit']))

I think you mean to use:

if(isset($_POST['submit']))

Issue 2

This SQL statement is wide-open to SQL injection:

mysql_query("SELECT * FROM tbl_book WHERE bookAuthor='$key'|| bookName='$key'||bookCode='$key' ")

You could improve security on this by using mysql_real_escape_string when you set $key like this:

$key=mysql_real_escape_string($_POST['txt_srchKey']);

However, this is not a perfect solution in all cases, when brings me to my final point.


Issue 3

mysql_query and all the other mysql_* are deprecated in the latest versions of PHP. See the warning on the mysql_query page and the MySQL: choosing an API for more information. You may want to consider using MySQLi or PDO as making secure database queries is much easier in these extensions.

Sign up to request clarification or add additional context in comments.

Comments

0

Try below :-

if(mysql_num_rows($all_entries) > 0){
<table border=1 align="left">
<tr>
<td><b>Book Code</b></td><td><b>Book Name</b></td><td><b>Edition</b></td><td><b>Author</b></td>
</tr>
<?php
while($row=mysql_fetch_array($all_entries))
{ ?>

<tr>
<td><?php echo $row['bookCode'] ?></td>
<td><?php echo $row['bookName'] ?></td>

<td><?php echo $row['bookEdition'] ?></td>
<td><?php echo $row['bookAuthor'] ?></td>
</tr>
<?php
}   
?>              
</table>
<?php } ?>

Comments

0

Before using mysql_fetch_array function always check records are coming from database or not

Using mysql_num_rows function

Example : if(mysql_num_rows($all_countries) > 0)

Add this code after your </table> tag

<?php   
      }   
       else  
       {         
          echo  "No Record Found";   
       }  
 ?>

if possible then use !empty instead of isset in your code

Comments

0

Prepend a condition before the while like this:

<?php 
    $number_of_results = mysql_num_rows($all_entries); 
    if ($number_of_results > 0) {
        while($row=mysql_fetch_array($all_entries)) { 
?>
<tr>
<td><?php echo $row['bookCode'] ?></td>
<td><?php echo $row['bookName'] ?></td>
<td><?php echo $row['bookEdition'] ?></td>
<td><?php echo $row['bookAuthor'] ?></td>
</tr>
<?php 
        } 
    } else {
?>   
<tr>
<td colspan="4">No results found</td>
</tr>
<?php
    }
?>

Please, be advised that the functions you're using right now are deprecated. They're also a common source for SQL injections and websites getting hacked. You could use something more adequate by using PDO or MySQLi.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.