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

后端 未结 30 2678
鱼传尺愫
鱼传尺愫 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 06:54
    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '%$username%'") or die(mysql_error());
    
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
    

    Sometimes suppressing the query as @mysql_query(your query);

    0 讨论(0)
  • 2020-11-21 06:54

    Make Sure You're Not Closing Database By using db_close() Before To Running Your Query:

    If you're using multiple queries in a script even you're including other pages which contains queries or database connection, then it might be possible that at any place you use db_close() that would close your database connection so make sure you're not doing this mistake in your scripts.

    0 讨论(0)
  • 2020-11-21 06:56

    Don't use the depricated mysql_* function (depricated in php 5.5 will be removed in php 7). and you can make this with mysqli or pdo

    here is the complete select query

    <?php
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "myDB";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "SELECT id, firstname, lastname FROM MyGuests";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            // code here 
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
    
    0 讨论(0)
  • If you tried everything here, and it does not work, you might want to check your MySQL database collation. Mine was set to to a Swedish collation. Then I changed it to utf8_general_ci and everything just clicked into gear.

    0 讨论(0)
  • 2020-11-21 06:57
    <?php
        $username = $_POST['username'];
        $password = $_POST['password'];
        $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '".$username."'");
    
        while($row = mysql_fetch_array($result))
        {
            echo $row['FirstName'];
        }
    ?>
    

    And if there is a user with a unique user name, you can use "=" for that. There is no need to like.

    Your query will be:

    mysql_query("SELECT * FROM Users WHERE UserName ='".$username."'");
    
    0 讨论(0)
  • 2020-11-21 06:57

    First, check your connection to the database. Is it connected successfully or not?

    If it's done, then after that I have written this code, and it works well:

    if (isset($_GET['q1mrks']) && isset($_GET['marks']) && isset($_GET['qt1'])) {
        $Q1mrks = $_GET['q1mrks'];
        $marks = $_GET['marks'];
        $qt1 = $_GET['qt1'];
    
        $qtype_qry = mysql_query("
            SELECT *
            FROM s_questiontypes
            WHERE quetype_id = '$qt1'
        ");
        $row = mysql_fetch_assoc($qtype_qry);
        $qcode = $row['quetype_code'];
    
        $sq_qry = "
            SELECT *
            FROM s_question
            WHERE quetype_code = '$qcode'
            ORDER BY RAND() LIMIT $Q1mrks
        ";
        $sq_qry = mysql_query("
            SELECT *
            FROM s_question
            WHERE quetype_code = '$qcode'
            LIMIT $Q1mrks
        ");
        while ($qrow = mysql_fetch_array($sq_qry)) {
            $qm = $qrow['marks'] . "<br />";
            $total += $qm . "<br />";
        }
        echo $total . "/" . $marks;
    }
    
    0 讨论(0)
提交回复
热议问题