0

Good day! I am having trouble displaying a "No records found" message in my PHP process.
Here is the code for my search query:

if(isset($_GET['submit'])) {

$product = $_GET['product'];
$city = $_GET['city'];

$query = "SELECT * FROM $product WHERE city = '$city'";
$result = mysqli_query($con, $query) or die ("Could not connect to database.");
$product = str_replace('_', ' ', $product);
$product = strtoupper($product);
   echo "You have searched for " . $product . " in " . $city;
   echo "<table border=1>";
   echo "<tr> <th>Store</th> <th>City</th> </tr>";
    while ($row = mysqli_fetch_array($result)) {
     echo "<tr><td>";
     echo $row['store'];
     echo "</td><td>";
     echo $row['city'];
     echo "</td></tr>";
    }
     echo "</table>";
}

My problem is I don't know where and what to place the conditional statement that will show "No records found".
Hoping that someone would be able to help me on this one.
Thanks in advance.

user3627135
  • 61
  • 2
  • 10
  • 1
    http://php.net/manual/tr/mysqli.affected-rows.php this is your answer. If there is 0 affected rows than you have no records. Simple :) – hakki Sep 15 '14 at 06:35
  • 1
    USE PDO on your projects. mysqli almost deprecated – hakki Sep 15 '14 at 06:53

3 Answers3

3

check if the number of rows in the result is 0 via mysqli_num_rows function and display the message before looping over the result set.

your code could be like

if(isset($_GET['submit'])) {

    $product = $_GET['product'];
    $city = $_GET['city'];

    $query = "SELECT * FROM $product WHERE city = '$city'";
    $result = mysqli_query($con, $query) or die ("Could not connect to database.");
    $product = str_replace('_', ' ', $product);
    $product = strtoupper($product);
    echo "You have searched for " . $product . " in " . $city;
    echo "<table border=1>";
    // check if results are present
    if(mysqli_num_rows($result)>0) {
        echo "<tr> <th>Store</th> <th>City</th> </tr>";
        while ($row = mysqli_fetch_array($result)) {
             echo "<tr><td>";
             echo $row['store'];
             echo "</td><td>";
             echo $row['city'];
             echo "</td></tr>";
        }
    } else {
        echo "<tr> <td colspan='2'> No Results found </td></tr>";
    }
     echo "</table>";
}
Mithun Satheesh
  • 27,240
  • 14
  • 77
  • 101
0

The mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

Aslo from reference:http://php.net/manual/tr/mysqli.affected-rows.php

An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.

    $product = $_GET['product'];
    $city = $_GET['city'];

    $query = "SELECT * FROM $product WHERE city = '$city'";
    $result = mysqli_query($con, $query) or die ("Could not connect to database.");
    $product = str_replace('_', ' ', $product);
    $product = strtoupper($product);
       echo "You have searched for " . $product . " in " . $city;    

    if(mysqli_affected_rows($con) ==0){ echo  "No records found"; }

    else{
       echo "<table border=1>";
       echo "<tr> <th>Store</th> <th>City</th> </tr>";
       while ($row = mysqli_fetch_array($result)) {
         echo "<tr><td>";
         echo $row['store'];
         echo "</td><td>";
         echo $row['city'];
         echo "</td></tr>";
        }
         echo "</table>";
    }
hakki
  • 6,181
  • 6
  • 62
  • 106
0

You should try this code , if any error any code in you code then you can find that.

$result = mysqli_query($con, $query) or die(mysqli_error($con));

OR
if(!$result){
echo die(mysqli_error($result));
}
Ankit
  • 259
  • 2
  • 13