0

I am using LIKE to do my searching, i try it in phpMyAdmin and return the result but when i use it in php it return empty result.

$search = "ip";
$start = 0;
$query = "SELECT * FROM product WHERE product_name LIKE '%$search%' LIMIT $start,30";
$result = mysql_query($query);
if(empty($result))
    $nrows = 0;
else
    $nrows = mysql_num_rows($result);

It will return result when i using phpMyAdmin to run this query but when i use it in php, it return empty.

Update:

Sorry guys,

I just found out the problem is i didn't connect database as well. anyway, thanks for helping.

ThiefMaster
  • 285,213
  • 77
  • 557
  • 610
Kelvin Go
  • 11
  • 3
  • If you check the value of $query, is it identical to what you used in phpmyadmin? – Adam Bergmark Apr 27 '11 at 09:19
  • Did you try to print out `$query` before execute it? Are you sure it's exactly written as you need? – Marco Apr 27 '11 at 09:19
  • What does `$search` contain? What does `$start` contain? Also note you will need to sanitize both values to protect against SQL injection http://stackoverflow.com/questions/601300/what-is-sql-injection – Pekka Apr 27 '11 at 09:20
  • @pekka +1 for SQL injection care. – Anji Apr 27 '11 at 09:22
  • try this `$query = "SELECT * FROM product WHERE product_name LIKE %".$search."% LIMIT $start,30";` – ianace Apr 27 '11 at 09:22
  • You haven't returned the result anywhere here in your code.. $results only stores the object of the resultset and you have either user mysql_fetch_array or mysql_fetch_object .. Make sure you included that.. – Vijay Apr 27 '11 at 09:26

6 Answers6

2

Try This

 $query = "SELECT * FROM `product` WHERE `product_name` LIKE '%".$search."%' LIMIT 0, 30";
Dipesh
  • 304
  • 5
  • 13
0
$search = "ip";
$start = '0';
$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";
$result = mysql_query($query)or die(mysql_error());
if(mysql_num_rows($result) == 0){
    $nrows = 0;
} else{
    $nrows = mysql_num_rows($result);
}

//use mysql_num_rows($result) instead of empty($result) because in this situation $result is every time not empty so use inbuilt PHP function mysql_num_rows($result);
ImBhavin95
  • 1,403
  • 2
  • 14
  • 25
  • Although this code may help to solve the problem, it doesn't explain _why_ and/or _how_ it answers the question. Providing this additional context would significantly improve its long-term educational value. Please [edit] your answer to add explanation, including what limitations and assumptions apply. – Toby Speight Oct 04 '16 at 15:03
  • @TobySpeight if you satisfy with my answer edit reply positively. thx bro. – ImBhavin95 Oct 05 '16 at 08:23
0

And if the sole purpose of your code is to get the number of products with the searched-for name, use SELECT COUNT(*) instead of doing a mysql_num_rows() on all your data. It will decrease your querytime and the amount of data that is (unnecessarily) fetched.

vindia
  • 1,678
  • 10
  • 14
0

I am not sure why this is not working, as the query seems to be correct to me. I would like to suggest you writing query this way

$query = <<<SQL

 SELECT * FROM product WHERE product_name LIKE "%$search%" LIMIT $start,30

SQL;

please note that there should not be any space or any character after SQL;

Anji
  • 725
  • 1
  • 9
  • 27
0
$query = "SELECT * FROM product WHERE product_name LIKE '%" . $search . "%' LIMIT " . (int) $start. ",30";
Flask
  • 4,891
  • 1
  • 18
  • 39
0

you can use directly mysql_num_rows()

but here is right code

$query = "SELECT * FROM product WHERE product_name LIKE '%".$search."%' LIMIT $start,30";

Noor Khan
  • 542
  • 1
  • 5
  • 12