- MySQL知识库 :: php
- Why is my PHP script returning nothing for a SELECT statement?
-
Discussion
If you have a PHP script or other API program that returns nothing for a simple SELECT statement, copy the SELECT statement from the script and paste it into the mysql client to try running it. If it doesn't produce an error, then you will know that the SQL statement is not the problem. Instead, it may be a problem with the connection to MySQL or your user/host combination.
Troubleshooting
Log into the mysql client again with the same user and password from the same host and then run the SQL statement. It could be that you have the user name misspelled or the password wrong in your PHP script, so copy and paste them or examine them carefully. If it's not a problem with spelling, it may be that the user from that particular host from where it's being run does not have access to the MySQL server. It could also be that that user/host account doesn't have SELECT privileges--this is very unlikely, though.
You should check your logs for error messages. If you're using a PHP script through the web browser, check the end of the server's Apache log (e.g., /var/log/httpd/error_log) right after you attempt to run the PHP script. This will probably give you an indication of what's wrong. Another method of getting feedback is to test your script from the command-line. If you're running a PHP script on a Linux server, just pass the page through the PHP interpreter so that you can run it. Here is an example of how to run it from the command-line:
php index.php
When you run it, you should see some error messages at the top of the display in addition to your HTML code and so forth.