php-mysql_num_rows警告[重复]

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select
PHP (MySQL) error : “Warning: mysql_num_rows() expects parameter 1 to be resource”

I am using the following code for uploading a file into the database. I am checking if the file already exists or not. But when i am checking i am getting a warning.

The following is my code.

    $filename = $_FILES["uploaded_file"]["name"];
    $location = "D:\\TrainingMaterials/";
    $path = $location . basename( $_FILES['uploaded_file']['name']); 
    $data = $mysqli->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
    move_uploaded_file( $_FILES["uploaded_file"]["tmp_name"], $location . $_FILES['uploaded_file']['name']);
    $q = "select Material_Name,Material_Data from training_material where Material_Name = '$filename' and Material_Data ='$data'";
    $rs = $mysqli->multi_query($q);
    if (mysql_num_rows($rs) == 0) 
    {
        $result = $mysqli->multi_query("call sp_upload_file('".$id."','" . $filename . "','".$path."','".$data."')");
        if ($result) 
        {
            do {
                if ($temp_resource = $mysqli->use_result()) 
                {
                    while ($row = $temp_resource->fetch_array(MYSQLI_ASSOC)) {
                        array_push($rows, $row);
                    }
                    $temp_resource->free_result();
                }
            } while ($mysqli->next_result());
        }
        echo "Successfully Uploaded";
    }
    else
    {
        echo "Material Already Uploaded";
    }

The warning i get is

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in......

Someone help me to find out what i am doing wrong.Thank you in advance

Instead of if(mysql_num_rows($rs) == 0), use if($rs->num_rows)

echo $q;

You will get a query execute it in phpmyadmin, I guess the query is wrong.

  • You're mixing mysqli (i as in improved) and mysql (as in deprecated api) functions.
  • Your code is prone to sql injections since you do not encode the parameters of the query properly. Since you're using multi_query() this script really is prone to bobby tables. Please search stackoverflow for good ways to prevent this.
  • In its current form the script only needs the count of records, not the actual data. In such a case use SELECT Count() instead retreiveing the actual data
  • Without locking this approach is prone to race conditions. Better use a unique constraint which will not allow the insertion of two identical (as defined by the key) records. Your script can then test for the specific error code to show the user the "item alread in database" message.

edit: the parameter $data is handled properly by $mysqli->real_escape_string.
But $filename is also "arbitrary data" as it can be virtually any string the client sends in the http request. So it must be escaped as well.