简体   繁体   中英

different result executing an sql query from phpmyadmin(good) and from PHP script(bad)

I have a really strange problem, executing a query on a MySQL DB:

  • from phpmyadmin: the query executes and return good results

  • from my PHP script: the query executes, no errors, good number of rows, but nearly 50% of the values shown in the table are wrong !

The SQL query used is exactly the same, rather long(250 lines), takes 26 seconds to execute in phpmyadmin, and works good.

On my PHP script the query iss executed with the mysqli_query() the typical way I would say:

if ($res=mysqli_query($mysqli,$q, MYSQLI_USE_RESULT)){
 $data = array();
  while ($row=mysqli_fetch_assoc($res)){
     $data[] = $row;
  }
  mysqli_free_result($res);
  var_dump($data); 
 }

On the query I replaced backtite: (`) to single quote: (').

Any idea why it's working with phpmyadmin, but not from PHP with mysqli_query() function? Btw phpmyadmin doesn't use the mysqli_query() function?

No errors is thrown, I'm really lost here, if someone has a hint, it would be much appreciated :)

Thx :)

The most likely answer is that something is different between the queries. As you cannot easily trace the difference in 250 lines. Use the MySQL query log to record both queries as MySQL sees them and then compare the differences:

To disable or enable the general query log or change the log file name at runtime, use the global general_log and general_log_file system variables. Set general_log to 0 (or OFF) to disable the log or to 1 (or ON) to enable it. Set general_log_file to specify the name of the log file. If a log file already is open, it is closed and the new file is opened.

Something along the lines of:

SET GLOBAL general_log = 'ON';
SET GLOBAL general_log_file= 'querylog.log';

.. run queries
 SET GLOBAL general_log = 'OFF';

Note that this is a global setting, the server will log queries from all clients whilst the log is enabled. Your MySQL also needs the super privilege to change this setting. Alternatively you can change in the my.cnf config file and restart MySQL.

Once you have the two queries, a comparison should show you where the difference is and guide you to a fix.

YAY after several headaches I solved by chance the mystery, I don't really know why though, but now it's working good.

Here are the small changes I made to make it work:

  1. change the PHP file encoding from UTF8 to ISO-Latin1
  2. retype all non alphha characters to be sure they're ok

In my SQL query I had an accent on a string I checked in a where clause, so I tried this..

The thing is I have many PHP files on the same server, encoded in UTF8 and text with accents that work perfectly.

The only difference here is that the accent is in my SQL query, maybe the server hosting my websites doesn't handle UTF8 characters properly and the system encoding is not well configured ..

Just to be sure, I have a terminal access to server, how can I see the system encoding ?

But problem solved, all good!

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