Question

PHP-MYSQL connection not working

Posted January 16, 2015 7.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:

<?php
$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);
$conn->setAttribute(PDO::ATTR
ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully”;
}
catch (PDOException $e) {
print “Error!: ” . $e->getMessage() . “<br/>”;
echo “Connection failed: ” . $e->getMessage();
die();
}
?>

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

file.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
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.

Submit an Answer