Ubuntu/PHP/MySQL: mysql-functions not working?
i recently installed Ubuntu on my server, replacing Windows. My Website ran fine under Windows/XAMPP, but not with Ubuntu. I tried a simple db-connect:
<?php
$con = mysql_connect("loca开发者_如何转开发lhost", "root", "mypassword") or die ("connection error");
mysql_select_db("db1", $con) or die ("db selection error");
$qry = mysql_query("SELECT * FROM atable");
while($row = mysql_fetch_object($qry)) {
echo $row->id;
}
?>
but when i load the page, nothing happens. php works fine,
<?php echo "test"; ?>
is working as expected...
my (testing purpose) settings are:
- Ubuntu 11.04 and LAMP
- the mysql bind-adress in my.cnf is set to 0.0.0.0
- the host of root-user of mysql is set to %, privileges are granted to .
- extension_dir of php5 is set to "/usr/lib/perl5/auto/DBD/mysql/mysql.so"
what am I missing/doing wrong?
Have you installed the php-mysql
package? If not please install it.In ubuntu you will find it in the Package Manager(search for php-mysql
) or do
sudo apt-get install php-mysql
After installing restart the webserver if necessary, using
sudo /etc/init.d/apache2 restart
You can find out, what you are doing wrong by looking at the error
if ($qry !== false) {
while($row = mysql_fetch_object($qry)) {
echo $row->id;
}
} else echo mysql_errno() . ': ' . mysql_error();
But remember, that the query may return an empty set, if your query simply not match any record in your database. In your case when atable
is empty, of course nothing can get returned.
Run the following code to see if you've got mysql support available within Apache and php:
<?php
if (function_exists('mysql_connect')) {
echo "Good<br />\n";
} else {
echo "Bad.<br />\n";
}
?>
If not you'll need to:
sudo apt-get install php5-mysql
and then restart Apache to get the code to work
sudo service apache2 restart
Source: http://bytes.com/topic/php/answers/869918-mysql_connect-function-isnt-working
精彩评论