Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
154 views
in Technique[技术] by (71.8m points)

php - Why is mysqli_query() breaking? [mysqli_query(): Couldn't fetch mysqli]

Mysqli_query is contained in the following encapsulating function in my code:

function QueryDatabase($query){
error_log("Database Line 134: " . memory_get_usage(true));
$QueryResult = NULL;

    switch($this->RDBType){
        case 'MySQL':
            error_log('MySQLi link IS Valid?:'); error_log(boolval($this->link));
            error_log(get_class($this->link));
            error_log("Database Line 139: " . memory_get_usage(true));
            error_log("Performing Query: '" . $query . "'");
            $QueryResult = mysqli_query($this->link, $query);
            error_log("Result: " . gettype($QueryResult));
            error_log("Database Line 141: " . memory_get_usage(true));
            break;
        case 'PostGreSQL':
        default:
            break;
    }
error_log("Database Line 147: " . memory_get_usage(true));  
return $QueryResult;
}

The PHP error log looks like this:

[11-May-2016 17:04:00 Europe/Berlin] Database Line 134: 262144

[11-May-2016 17:04:00 Europe/Berlin] MySQLi link IS Valid?:

[11-May-2016 17:04:00 Europe/Berlin] 1

[11-May-2016 17:04:00 Europe/Berlin] mysqli

[11-May-2016 17:04:00 Europe/Berlin] Database Line 139: 262144

[11-May-2016 17:04:00 Europe/Berlin] Performing Query: 'SELECT * FROM scans INNER JOIN landlot on scans.scansId = landlot.scansId INNER JOIN district on scans.scansid = district.scansId INNER JOIN streetname on scans.scansid = streetname.scansId WHERE Lot = 1;'

[11-May-2016 17:04:00 Europe/Berlin] PHP Warning: mysqli_query(): Couldn't fetch mysqli in I:xampphtdocsGLS_DBSearchProjectDatabase.php on line 145

[11-May-2016 17:04:00 Europe/Berlin] Result: NULL

[11-May-2016 17:04:00 Europe/Berlin] Database Line 141: 262144

[11-May-2016 17:04:00 Europe/Berlin] Database Line 147: 262144

[11-May-2016 17:04:00 Europe/Berlin] PHP Notice: Trying to get property of non-object in I:xampphtdocsGLS_DBSearchProjectDatabaseSearch.php on line 86

[11-May-2016 17:04:00 Europe/Berlin] Database Line 134: 262144

[11-May-2016 17:04:00 Europe/Berlin] MySQLi link IS Valid?:

[11-May-2016 17:04:00 Europe/Berlin] 1

[11-May-2016 17:04:00 Europe/Berlin] mysqli

[11-May-2016 17:04:00 Europe/Berlin] Database Line 139: 262144

[11-May-2016 17:04:00 Europe/Berlin] Performing Query: 'SELECT * FROM scans INNER JOIN landlot on scans.scansId = landlot.scansId INNER JOIN district on scans.scansid = district.scansId INNER JOIN streetname on scans.scansid = streetname.scansId WHERE Lot = 1 LIMIT 10 OFFSET 0;'

[11-May-2016 17:04:00 Europe/Berlin] PHP Warning: mysqli_query(): Couldn't fetch mysqli in I:xampphtdocsGLS_DBSearchProjectDatabase.php on line 145

[11-May-2016 17:04:00 Europe/Berlin] Result: NULL

[11-May-2016 17:04:00 Europe/Berlin] Database Line 141: 262144

[11-May-2016 17:04:00 Europe/Berlin] Database Line 147: 262144

[11-May-2016 17:04:00 Europe/Berlin] PHP Fatal error: Call to a member function fetch_assoc() on a non-object in I:xampphtdocsGLS_DBSearchProjectDatabaseSearch.php on line 126

I'm really confused as to why mysqli_query is failing to return a result. I have used my QueryDatabase() function numerous times throughout my project and have encountered no issues until now. I have a PHPUnit test which shows that my QueryDatabase() function appears to be working properly, and all other signs point to the problem occurring during the mysqli_query() function call.

I have extensively checked to make sure that the database link (the mysqli object contained within $this->link) is valid and points to the appropriate database. I have also checked to ensure that my query works as expected by manually copying and pasting it into the mysql query browser.

If my query is valid AND my database link is valid why could mysqli_query() be failing?

Edit: I never closed any of my database connections. So the database connection should not be closed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is nothing wrong with your logs.

A live mysqli instance doesn't mean there is a live mysql connection. Unlike PDO, you can close mysql connection but have a mysqli object all right. So the error message says:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli('localhost','root','','test');
$conn->query("SELECT 1");
$conn->close();
var_dump(get_class($conn));
$conn->query("SELECT 1");

will give you expected output:

string(6) "mysqli"
Warning: mysqli::query(): Couldn't fetch mysqli in mysqli.php on line 10

You have to find the place where your code is closing connection and fix it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...