简体   繁体   中英

PHP MySQL Query of view returns 0 results when there should be a result

I have a site that is running PHP 5.6. The database for the site was recently upgraded from MySQL 5.1 to MariaDB 10.0 (MySQL 5.5 Compatible) and now a query on my site won't work (and it was working without error when connecting to the MySQL 5.1 database.

For this demo, I have a database table called "content" with fields "id" (integer), and "page_title" (var_char). In that table are two rows - one with an ID of 1 and page_title of "Test 1", and the second row has ID of 2 and page_title set to "Test 2".

I then created a view of this table using the following query inside of phpMyAdmin:

select `content`.`id` AS `id`,`content`.`page_title` AS `page_title` from `content`

I can see the view and both rows from the content table in the view in phpMyAdmin.

On my PHP page, I have a object oriented style query. That code is:

<?php
try {
require_once 'Connections/dbconn.php';
$sql = "SELECT id, page_title FROM v_content WHERE id = 1 LIMIT 1";
$stmt = $db->stmt_init();
if (!$stmt->prepare($sql)) {
    $error = $stmt->error;
} else {
    $stmt->bind_result($id, $page_title);
    $stmt->execute();
    $stmt->store_result();
    $stmt->fetch();
}
} catch (Exception $e) {
$error = $e->getMessage();
}?>

This query is returning the following: Attempt to read a row while there is no result set associated with the statement

If I change the table from the VIEW "v_content" to just the table "content", I get a result returned. Additionally, if I use the same query (querying the VIEW v_content), and connect to a MySQL 5.1 database, I get results returned. I know there is nothing wrong with the query itself, as I have copied and pasted the query into phpMyAdmin and have had results returned, and the exact same code was used on a MySQL 5.1 database and that returned results. I have checked the error logs and there are no errors being shown.

Anyone have any idea what could be causing me to get 0 results returned from the view?

Looks like after all, there was no issue on my end with the actual coding of the site. The hosting company gave me the following explanation:

It appears that the issue with prepared statements working incorrectly with views was related to the table_definition_cache size on the new maria database servers. This is a global setting, and essentially, if the number of tables put into cache exceeds this value, it starts flushing older entries out of the cache. It appears that if a statement is prepared, but the entries leave the table_definition_cache before the prepared statement is executed, it's considered "invalid" and needs to be re-prepared. Since this is a global value, the accessing of others tables can reset its counter. As a result, we increased the value of table_definition_cache to a much higher value on all of the database servers, which has resolved the issue.

I think you just need to switch the places of execute and bind.

...
} else {
    $stmt->execute();
    $stmt->bind_result($id, $page_title);
    $stmt->store_result();
    $stmt->fetch();
}
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM