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

后端 未结 30 2697
鱼传尺愫
鱼传尺愫 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:01

    Please check once the database selected are not because some times database is not selected

    Check

    mysql_select_db('database name ')or DIE('Database name is not available!');
    

    before MySQL query and then go to next step

    $result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
    
    f($result === FALSE) {
        die(mysql_error());
    
    0 讨论(0)
  • 2020-11-21 07:01

    Include a connection string variable before the MySQL query. For example, $connt in this code:

    $results = mysql_query($connt, "SELECT * FROM users");
    
    0 讨论(0)
  • 2020-11-21 07:04

    Put quotes around $username. String values, as opposed to numeric values, must be enclosed in quotes.

    $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");
    

    Also, there is no point in using the LIKE condition if you're not using wildcards: if you need an exact match use = instead of LIKE.

    0 讨论(0)
  • 2020-11-21 07:04

    Your code should be something like this

    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = "SELECT * FROM Users WHERE UserName LIKE '$username'";
    echo $query;
    $result = mysql_query($query);
    
    if($result === FALSE) {
        die(mysql_error("error message for the user")); 
    }
    
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
    

    Once done with that, you would get the query printed on the screen. Try this query on your server and see if it produces the desired results. Most of the times the error is in the query. Rest of the code is correct.

    0 讨论(0)
  • 2020-11-21 07:05

    Go to your config.php. I had the same problem. Verify the username and the password, and also sql select is the same name as the config.

    0 讨论(0)
  • 2020-11-21 07:05

    Try This

    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = mysqli_query('SELECT * FROM Users WHERE UserName LIKE $username');
    
    if($result){
    while($row = mysqli_fetch_array($result))
    {
        echo $row['FirstName'];
    }
    }
    
    0 讨论(0)
提交回复
热议问题