PHP: Unknown MySQL server host with mysqli
I have the following code:
$username = 'username';
$password = 'password';
$host = 'localhost';
$db = 'dbname';
$dbh = new mysqli('p:'.$host, $username, $password, $db)
or die('no connection to server');
But I get the following error
PHP Warning: mysqli::mysqli() [<a href='function.mysqli-mysqli'>function.mysqli-mysqli</a>]: (HY000/2005): Unknown MySQL server host 'p:localhost' (1) in /var/www/vhosts/politiker.lu/httpdocs/includes/sql.php on line 8
开发者_如何学编程
This intrigues me because the very same code worked on my development environment. Now that I want to set it up on my production server, it does not work. Any ideas?
Note: I am not very good with servers, so excuse me if the error should be something trivial.
EDIT 1
Here are the versions: Development PHP (5.3.3-1ubuntu9.1) + MySQL(5.1.49-1ubuntu8.1)
Production PHP (5.2.4-2ubuntu5.12) + MySQL(5.0.51a)take out the p:
like this:
$dbh = new mysqli($host, $username, $password, $db)
or die('no connection to server');
Just adding this since I didn't think it was clear, but the problem is definitely your production version of PHP. Persistent connections for the mysqli extensions weren't added until 5.3 since they caused headaches before.
Some hosts might have different versions of PHP installed... I realized today that I can make HostGator use PHP 5.3! I just had to add to my .htaccess file:
Action application/x-hg-php53 /cgi-sys/php53
AddType application/x-hg-php53 .php
i think you missed the mysqli package in this server. Try :
sudo apt-get install php5-mysqli
if apt-get says it is already installed try to do a ping to localhost, if don't resolve edit /etc/hosts and check the line where localhost points to 127.0.0.1 should be there, if not, your server its horribly configured!
do a
ifconfig
and check if you get some output like this...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
You need to specify the actual location of the MySQL server. For development, it is localhost
(most likely), just how you have it configured. But for production servers, it is going to be something else. I doubt it should be localhost
.
精彩评论