php, mysqli problem
my server is centos (CentOS release 5.6 (Final) ).
I have a strange problem with mysqli . the mysqli connection only works with command line script , not web page script.
I hav开发者_如何学JAVAe fiollowing code inside my php script test.php:
$connection = mysqli_init();
mysqli_real_connect($connection,$host,$user,$pass,$database);
var_dump(mysqli_connect_errno(), mysqli_connect_error());
so when run that file under command line, it is ok, but in web page, it return error code "2003", and error message Can't connect to MySQL server on xxxxxx
linux version is 2.6.18-238.19.1.el5xen #1 SMP Fri Jul 15 08:16:59 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux
This is just a "gut feeling" guess; frankly you're not giving enough info to actually work with.
My guess is that you're connecting to "localhost" as your hostname. In Linux this will attempt to use a unix (file-based) socket. When you run from the command line, you're probably running as a different user than when you're running it from the web server.
I'm guessing that your web-server user (www-data or apache or something) probably doesn't have permission to access the socket file. Try changing from "localhost" to "127.0.0.1" (yes, they are treated differently) and see if you can connect.
oh, centos use Selinux which does not allows apache to connect to remote host by default. that reason why it not working in web page.
changed selinux setting, it working now.
精彩评论