PHP-MYSQL connection not working

January 16, 2015 4k views

Hello Eveyone,

I have just started working with php-mysql besides my daily job and since few days I am banging my head resolving the connection problem between Apache, PHP and MySQL.

Firstly I deinstalled all the PHP and mysql things that comes with Linux distro and then installed mysql 5.6.22 and php 5.6.4 on my rhel6 machine and configured php

from source using below command.

./configure --with-apxs2=/usr/sbin/apxs --with-mysql

After this I wrote a some code to check connection between mysql and php. Here it goes:

$host_name = "localhost";
$database = "rating"; // Change your database name
$username = "rate"; // Your database user id
$password = "rate"; // Your password

//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host='.$hostname.';dbname='.$database, $username, $password);
echo "Connected successfully";
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
echo "Connection failed: " . $e->getMessage();

This throws me an error driver not found. After googling i came to know there could be missing PDO drivers and have to install that. Please help me to resolve this.

How toget my php-mysql connection working. Please help. Is my configuration command above incorrect. Please guide me and what all changes need to be done in php.ini


2 Answers

What OS have you got?

If its centos.. try:
yum install php-pdo php-mysqli

restart apache

Try this:

sudo apt-get install php5-mysqlnd

To install a further php5 package.

Have another answer? Share your knowledge.