PHP : Warning: mysql_num_rows() expects parameter 1 to be resource

+1 Ruwan Ranganath · December 3, 2014
Hi ,

I was following this video :

and when i checked at Time : 3.48 , it's showing


Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\server\htdocs\PhpLearning\search\index.php on line 12


my Index.php code


<?php 
require './connection.inc.php';

if (isset($_POST['search_name'])) {
    $search_name = $_POST['search_name'];
    if (!empty($search_name)) {
         
        $query = "SELECT 'cousename' FROM 'courses' WHERE 'cousename' "
                . "LIKE '%".mysql_real_escape_string($search_name)."%'";

        $query_run = mysql_query($query);
        
        if (mysql_num_rows($query_run) >=1) {
            echo 'FOUND';
        }  else {
            echo 'Not Found .';
        }
    }
    
}

?>

<form action="index.php" method="POST">
    Name :<input type="text" name="search_name" />
    <input type="submit" value="Search">
</form>


In case if needed : connection.inc.php



<?php
$conn_error = 'Could not connect,';

$mysql_host = 'localhost';
$mysql_user ='root';
$mysql_pass ='123';

$mysql_db ='myDatabaseName';

if (!@mysql_connect($mysql_host, $mysql_user, $mysql_pass) || !@mysql_select_db($mysql_db)) {
    die($conn_error);
}
?>



Post a Reply

Replies

Oldest  Newest  Rating
+1 Franz Schmidt · December 3, 2014
I don't know why, but MySQL seems to have a problem with single quotes at the database.
I never use single quotes until I really need them (like if I assign a value)

Just change your query to:
$query = "SELECT cousename FROM courses WHERE cousename "
. "LIKE '%".mysql_real_escape_string($search_name)."%'";
0 Ruwan Ranganath · December 3, 2014
@Franz Schmidt : thank you very much for your helpful answer , it's working properly , honostly i wasted almost more than 5 hours because of that , and when I posted at Stackover flow i only got answers like
   - duplicate thread  , -  wrong table ,  - use PDO , MySQLi  . etc
your help really appreciate.
+1 Franz Schmidt · December 3, 2014
No problem:D
I also had this error several times before and a little hint:
If you have a mysql error use this:
e.g. you have a if statement with a query
if($query){
//some code
}else{
die(mysql_error());
}

This will give you some better information than just a "warning"
  • 1

PHP

107,039 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators