开发者

mysql_query() is FALSE but mysql_error() is blank

开发者 https://www.devze.com 2023-03-28 12:20 出处:网络
I\'m running pretty complicated query that is built from a few inner queries. The query contained SQL_CALC_FOUND_ROWS at the beginning of it. Unfortunately only number of rows could be fetched (I coul

I'm running pretty complicated query that is built from a few inner queries. The query contained SQL_CALC_FOUND_ROWS at the beginning of it. Unfortunately only number of rows could be fetched (I couldn't fetch result itself).

I decided to get rid of SQL_CALC_FOUND_ROWS and run two different queries, one that counts rows and second that fetches them. In this case I can retrieve results but COUNT(*) query acts strangely. This is what I do:

  1. Perform COUNT(*) query
  2. mysql_query() returns FALSE
  3. mysql_error() run directly after the COUNT(*) query returns blank string

The开发者_开发知识库 code is:

$sqlCount = "SELECT COUNT(*) AS c FROM ... WHERE ...";
$resultCount = mysql_query($sqlCount);
echo "RESULT COUNT: " . ($resultCount === FALSE) . "<br />";//it returns 1(true)
echo mysql_error(); //it returns nothing (blank string)

ADDITIONALLY mysql_errno() returns 0

Could you find any reasonable explanation of such a strange situation?


I do not see any mistakes in your code. Please try to change your mysql_query line to

 $resultCount = mysql_query($sqlCount) or die(mysql_error() ." <-- There was an error when proccessing query");

It looks weird but could help you


It's strange because select clause with SQL_CALC_FOUND_ROWS returns rows and additional query will return number of rows. If you use

$result = mysql_query('SELECT SQL_CALC_FOUND_ROWS * from table');
$total = mysql_query('SELECT FOUND_ROWS()');
process $result.

you shouldn't get any problem.


Could you please fill out the ...gaps and try

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$mysql = mysql_query('..', '..', '..') or die(mysql_error());
mysql_select_db('...', $mysql) or die(mysql_error($mysql));

echo '<pre>Debug: mysql_get_server_info='; var_dump(mysql_get_server_info($mysql)); echo "</pre>\n";
echo '<pre>Debug: mysql_get_client_info='; var_dump(mysql_get_client_info()); echo "</pre>\n";

$sqlCount = "SELECT COUNT(*) AS c FROM ... WHERE ...";
echo '<pre>Debug: sqlCount='; var_dump($sqlCount); echo "</pre>\n";
$resultCount = mysql_query($sqlCount, $mysql);
echo '<pre>Debug: resultCount='; var_dump($resultCount); echo "</pre>\n";
echo '<pre>Debug: mysql_error='; var_dump(mysql_error($mysql)); echo "</pre>\n";
echo '<pre>Debug: mysql_errno='; var_dump(mysql_errno($mysql)); echo "</pre>\n";

and post the output?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号