简体   繁体   中英

php can't see local mysql server

I'm just getting started trying to set up a server/website and have run into what is probably a very basic issue. I'm getting a blank screen when I try to connect to my installed mysql server with a php script; eg

<html>
<body>
<script src="http://protovis-js.googlecode.com/svn/trunk/protovis-r3.1.js" type="text/javascript"></script>

<?php 
echo "Connecting..."
$user="root";
$password="passwd";
$server="127.0.0.1";
mysql_connect($server,$user,$password);
echo "Connected.";

?>
</body></html>

shows "Connecting..." but never reaches "Connected". There's nothing wrong with the code or database, since this did work within the browser in Aptana 2.05 (since uninstalled). It doesn't work within Firefox or Chrome. Aptana was using a different (older) version of PHP, 5.2; phpinfo() accessed from Chrome/Firefox was 5.3. The php.ini file contains extension=mysql.so; otherwise I couldn't tell if the settings were correct or not. The tutorials I looked at didn't really cover this sort of thing... anybody know what to do?

What browser you're using has nothing to do with whether the php script running on the server connects to a database or not.

I would suggest checking the server logs, or adding some code to actually check and see what's going on:

$link = mysql_connect($server,$user,$password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

Your MySQL instance may be configured to not respond to localhost. There are two ways of connecting to a MySQL server; through a network connection (ip and port) or socket connection, which only works on the machine it's working on. Your MySQL server may be configured to listen to socket connections but not network.

  1. Try connecting using the mysql command line tool as "mysql -h 127.0.0.1 -uroot -ppassword mysql"
  2. Try running your PHP script from the command line instead of inside the browser.

Ha! Just didn't have the php5-mysql package installed. So evidently having php and mysql by themselves is not enough. Installed php5-mysql, the connection worked.

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