Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 456
user2762748 Avatar asked Nov 02 '15 19:11

user2762748


People also ask

What does MySQL return if nothing is found?

SQL – Return 0 When results are empty. Display data in SSRS. sql select a default row if the result is not found. Adding dummy data to a column in SQL Server.

How check query result is empty in PHP?

PHP empty() Function The empty() function checks whether a variable is empty or not. This function returns false if the variable exists and is not empty, otherwise it returns true.

How do I assign a default value if no rows returned from the select query MySQL?

You can use IFNULL() function from MySQL to return a value even if there is not result. Let us create a table.

How is it possible to know the number of rows returned in the result set in PHP?

The mysqli_num_rows() function returns the number of rows in a result set.


1 Answers

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.

like image 70
user2762748 Avatar answered Oct 17 '22 03:10

user2762748