3

I wrote a php search for a table in mysqli and it works fine but i want to show the correct message to user if no result were found.

here is my current code:

$search_string=$_GET["design"];

$connect= mysqli_connect("mysql.myhost.com","abc","123456","mydb_db");
$query="select * from product where product_design like '%$search_string%'";
$rows= @mysqli_query($connect,$query) or die("Error: ".mysqli_error($connect));
if ($rows!=null)//I put this if to check if there is any result or not but its not working
{

while(($record=mysqli_fetch_row($rows))!=null)
{
    .
            .//i have working code for showing the result here
            .
}   
mysqli_close($connect);
}
else{
echo"no result found";
}

could you please help me what is wrong , even when i search for something which is not exist in the db still the program not displaying "no result found"

Thank you

David83
  • 45
  • 1
  • 3
  • 1
    try changing `$rows!=null` to `$rows > 0` – lemonpole Oct 23 '11 at 04:38
  • 1
    And hope no one runs the GET request with design=`';DROP TABLE product;` – Phil Lello Oct 23 '11 at 04:44
  • To expand on @Phil's point, your code is very vulnerable to [SQL Injection](http://php.net/manual/en/security.database.sql-injection.php) attacks. This means that anyone using your site could do anything to your database. Consider using parameterised queries (Google It), – Basic Oct 23 '11 at 05:15

2 Answers2

3

What you need is mysqli_num_rows specifically the mysqli_result::num_rows bit. This adds a num_rows property to mysqli result sets. This means you can do

$rowCount = $rows->num_rows

There's also a non-OO equivalent ...

$rowCount = mysqli_num_rows($rows);

(The difference is purely one of coding style)

Use one of these to determine how many records are returned and output the appropriate messages.

Basic
  • 26,321
  • 24
  • 115
  • 201
  • Thank you but the thing is when i put the echo to check the number of rows if i leave the text box blank the num_rows is 12 cause i have 12 records in the table. but if i put one of the existing rows as a search it will return 1 cause only one row is match. it is starnge – David83 Oct 23 '11 at 05:05
  • It sounds like it's working perfectly... the number of rows is only ever the number of rows sent back by the database, never the number of rows stored in the database (unless you get all rows of course). – Basic Oct 23 '11 at 05:10
  • 1
    If you want to know the total number of rows in the table, do a second query for `SELECT COUNT(*) AS TotalRecordCount FROM product`which will return one field (called `TotalRecordCount`) with one row which will contain the number of rows in the table. – Basic Oct 23 '11 at 05:13
  • I thought that using mysqli_num_rows was the correct way to check to see if a result set was empty but in the latest PHP5 (PHP 5.5.12) gives PHP Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /var/www/scripts/common_php.php on line 20 – Michael Fever Jun 01 '14 at 07:17
  • @MichaelDeMutis I've moved on from PHP since writing this. I suggest you open a new question referencing this one. – Basic Jun 01 '14 at 08:42
0

The following line doesn't make sense, that might be the issue.

while(($record=mysqli_fetch_row($rows))!=null)

However, $row wouldn't return 'null' if it was empty, but be not set. Do it like this:

if ($rows) { echo 'works';} else { echo 'no rows'; }
Anonymous
  • 3,679
  • 6
  • 29
  • 40