Hello everyone :) I have this lines of codes and i get :

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in D:\xampp22\htdocs\folder\index.php on line 52 ( 19 in the code below )

<form method="POST" action="index.php">
    CODE : <input type="text" placeholder="Type the code" name="searchfor" /> 
    <input type="submit" name="submit" value="Search for the code">
</form>
<?php



if(isset($_REQUEST['submit'])){
    $searchfor = $_REQUEST['searchfor'];
    $terms = explode(",", $searchfor); // i've deleted this and no-positive results , i was thinking that it gives a false value to the code :-??

    $query = "SELECT * FROM edit WHERE id=$searchfor" ; // i've tried also with WHERE id LIKE $searchfor

include ("link_to_connection_to_sql.php"); // it work's , it's tested


$query = mysql_query($query);
$num = mysql_num_rows($query); // here is the error

if($num > 0 && $searchfor != ''){
    while ( $row = mysql_fetch_assoc($query)){
        $price = $row['price'];
        echo "$price";
    }}else{
        echo 'No results';
    }
}
 else{
    echo 'Please fill the search form ';
}





?>

I will be gratefull if you help me with an explanation and a solution :) Thank you ! (I don't really understand the error , I'm at the begin )

  • : If it helps , the table is edit with 2 columns id , price .. this should be a search by id and give the price of the id :D
Member Avatar for iamthwee

most likely you need to put single quotes:

$query = "SELECT * FROM edit WHERE id='$searchfor'"

Member Avatar for diafol

Yep that should do it.

Also:

$query = mysql_query($query);
$num = mysql_num_rows($query); // here is the error
if($num > 0 && $searchfor != ''){
    while ( $row = mysql_fetch_assoc($query)){
        $price = $row['price'];
        echo "$price";
    }}else{
        echo 'No results';
    }
}
 else{
    echo 'Please fill the search form ';
}

Consider changing the name of the resource ($result):

$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num > 0 && $searchfor != ''){
    while ( $row = mysql_fetch_assoc($result)){
        $price = $row['price'];
        echo "$price";
    }
}else{
    echo 'No results';
}

Using a variable called $query suggests that the content is an SQL string.
In addition, you are using mysql, which is set to be dropped in the future. It may be worth your while adopting mysqli or PDO.
Anyway, you're using unsanitized data in your SQL, which can be very dangerous - so you need to clean it with mysql_real_escape_string().

Also, you seem to be 'posting' your search term, so use $_POST not $_REQUEST - unless you want to accept $_GET and $_COOKIE too.

Thank you for all suggestions !
@diafol - Yes , i know that mysql will be dropped soon or later , but .. i must start from somewhere , no?

Whatever , the error keep showing ( Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in index.php on line 52 ) with this code:

<?php



if(isset($_POST['submit'])){
    $searchfor = $_POST['searchfor'];
    $terms = explode(",", $searchfor);

    $query = "SELECT * FROM edit WHERE id='$searchfor'" ;

include ("link_to_connection_to_sql.php");


$result = mysql_query($query);
$num = mysql_num_rows($result);
if($num > 0 && $searchfor != ''){
    while ( $row = mysql_fetch_assoc($result)){
        $price = $row['price'];
        echo "$price";
    }
}else{
    echo 'No results';
}


}

?>
Member Avatar for iamthwee

What happens when you do:

$result = mysql_query($query) or die(mysql_error());

I've found the error with your help iamthwee , thank you :)

Member Avatar for iamthwee

Please mark as solved Ta.

<?php

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

?>

This is an example on php.net: "search for mysql_num_rows in the function list"

It works fine for me
only there is no WHERE id='$searchfor'

(The query string should not end with a semicolon. Data inside the query should be properly escaped.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.