mysql_fetch_array() returns 'supplied argument is not a valid MySQL result resource'

前端 未结 4 1704
陌清茗
陌清茗 2020-12-22 04:38

I am trying the following code:



        
相关标签:
4条回答
  • 2020-12-22 05:25

    Your MySQL query possibly does not match any rows in the database.

    Check the return value of mysql_query(), which returns "resource" on success and "false" on failure.

    $query = "SELECT * FROM Auctions"; 
    $result = mysql_query($query);
    
    if ($result !== false) {
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 
            foreach ($row as $field=>$value) { 
                echo $field . ':' . $value
            }
        }
    } else {
        // query returned 0 rows
    }
    

    As others also suggested, you can use mysql_error() to look if the query returns any mySQL errors

    0 讨论(0)
  • 2020-12-22 05:31

    Are you getting anything returned? If no results are found, mysql_query returns FALSE.

    Check that before running fetch_array.

    0 讨论(0)
  • 2020-12-22 05:34

    You haven't selected a database - use mysql_select_db()

    That would be something like:

    <?php
        $link = mysql_connect('localhost', 'root', 'geheim');
        if (!$link) {
            die('Could not connect: ' . mysql_error());
        }
        echo 'Connected successfully';
    
        $db_selected = mysql_select_db('foo', $link);
        if (!$db_selected) {
            die ('Error selecting database: '. mysql_error());
        }
        echo 'Using database successfully';
    
        $query = "SELECT * FROM Auctions";
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            foreach($row as $field=>$value) {
                echo "$field: {$value} <br />";
            }
        }
        mysql_close($link);
    ?> 
    
    0 讨论(0)
  • 2020-12-22 05:43

    $query = "SELECT * FROM Auctions";

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

    so you'll see the error

    0 讨论(0)
提交回复
热议问题