Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result

后端 未结 6 827
耶瑟儿~
耶瑟儿~ 2020-11-21 05:35

I get the error when trying to run this:

query(         


        
相关标签:
6条回答
  • 2020-11-21 05:49

    Your query must have a problem which is causing $result to be an invalid resource.

    Try checking for mysql_error() after the line on which you run your query.

    Edit:

    In fact, I would alter your DBConnector class function query() to something like the following, so that an identifiable error is thrown when you have a bad query:

    function query($query) {
        $this->theQuery = $query;
        $queryId = mysql_query($query,$this->link);
        if (! $queryId) {
            throw new Exception(mysql_error().".  Query was:\n\n".$query."\n\nError number: ".mysql_errno();
        }
        return $queryId;
    }
    
    0 讨论(0)
  • 2020-11-21 05:51

    Your query must have a problem which is causing $result to be an invalid resource.

    Use this

    <?php
    require_once('includes/DbConnector.php');
    $connector = new DbConnector();
    $result = $connector->query('SELECT title,content FROM staff_vacancies ORDER BY ordering LIMIT 0,100');
    // Get an array containing the results.
    // Loop for each item in that array
    
    if($result){
    while ($row = $connector->fetchArray($result)){
    
    echo $row['title'].'</h3>';
    echo $row['content'];
    }
    }
    ?>
    
    0 讨论(0)
  • 2020-11-21 05:54

    This error means your query failed. mysql_query() returns false if an error occurred, you are then passing false to mysql_fetch_array() which is triggering the error message.

    Your query could be failing due to a missing/wrong table or field. To see the detailed error, print out the result of mysql_error().

    The mysql_* library is deprecated. It is recommended to upgrade to MySQLi or PDO.

    0 讨论(0)
  • 2020-11-21 05:54
    // Load settings from parent class
    $settings = SystemComponent::getSettings();
    
    // Get the main settings from the array we just loaded
    $host = $settings['dbhost'];
    $db = $settings['dbname'];
    $user = $settings['dbusername'];
    $pass = $settings['dbpassword'];
    
    //the settings
    $host = 'localhost';
    $db = 'xxx';
    $user = 'xxx';
    $pass = 'xxx';
    

    Did you mean to reassign the connection vars? OR was that a few lines of stub code you forgot to take out? Or just an example to show what $settings contains?

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

    I find this in a post, to me solved my problem. Slds.

    Yeah, Answer is simple, the query used is not a true result as it's a query inside of a getrow so to speak.. Here is the fix: Find all lines that look like this:

    mysql_fetch_array(mysql_query("...snip...";-) );
    

    And just add a "@" in front of it so it looks like this:

    @mysql_fetch_array(mysql_query("...snip...";-) );
    

    Then do the same thing for the following lines.. Code:

    mysql_num_rows(mysql_query("...snip...";-) );
    

    Perform the same steps as above by adding the "@" to it so it looks like this:

    @mysql_num_rows(mysql_query("...snip...";-) );
    

    All this does it say "While doing xxx within yyy" Otherwise, it's dead due to missing result value. It's a PHP thing..

    Works like a charm, took me 5 mins to rip the whole code apart and slap it all into Modernbill, Shares the same database and works perfectly for me.

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

    Please provide the error from mysql_error(). Without that I can only guess... try escaping your field names?

    $result = $connector->query('SELECT `title`,`content` FROM `staff_vacancies` ORDER BY `ordering` LIMIT 0,100');
    
    0 讨论(0)
提交回复
热议问题