mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource

后端 未结 30 2778
鱼传尺愫
鱼传尺愫 2020-11-21 06:25

I am trying to select data from a MySQL table, but I get one of the following error messages:

mysql_fetch_array() expects parameter 1 to be resource,

30条回答
  •  眼角桃花
    2020-11-21 07:08

    Usually an error occurs when your database conectivity fails, so be sure to connect your database or to include the database file.

    include_once(db_connetc.php');
    

    OR

    // Create a connection
    $connection = mysql_connect("localhost", "root", "") or die(mysql_error());
    
    //Select database
    mysql_select_db("db_name", $connection) or die(mysql_error());
    
    $employee_query = "SELECT * FROM employee WHERE `id` ='".$_POST['id']."'";
    
    $employee_data = mysql_query($employee_query);
    
    if (mysql_num_rows($employee_data) > 0) {
    
        while ($row = mysql_fetch_array($employee_data)){
            echo $row['emp_name'];
        } // end of while loop
    } // end of if
    
    • Best practice is to run the query in sqlyog and then copy it into your page code.
    • Always store your query in a variable and then echo that variable. Then pass to mysql_query($query_variable);.

提交回复
热议问题