PHP not connecting to a MySQL database server

by on May 16, 2006 · 11 comments· LAST UPDATED November 10, 2007

in , ,

Q. I have Apache Web server and MySQL configured. Everything works fine except I am not able to connect to a MySQL database server (I am able to connect mysql server with mysql client). So how do I configure or connect PHP scripts?

A. You need to install MySQL module for php4 or php5. Without php-mysql shared object or module, php scripts won't able to connect to MySQL database server.

Debian / Ubuntu Linux User

To install type the following command:
# apt-get install php4-mysql
if you are using PHP5, enter:
# apt-get install php5-mysql

RHEL <= 4 user

If you are using Red Hat Linux:
# up2date php-mysql

If you are using Fedora / CentOS / RHEL 5 Linux:
# yum install php-mysql
Restart apache to take effect. Now you will be able to connect MySQL via PHP scripts.
# service httpd restart

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 11 comments… read them below or add one }

1 rajesh December 20, 2007 at 4:25 pm

I m not able to connect localhost. error 3306

Reply

2 Bill Brinegar January 22, 2009 at 11:19 pm

I reat the following message and am having the same problem. I used XAMPP to install the group for windows vista. I have also installed them individually for the internet and also from a disk I got from a SAMs book.

I have Apache Web server and MySQL configured. Everything works fine except I am not able to connect to a MySQL database server (I am able to connect mysql server with mysql client). So how do I configure or connect PHP scripts?

I don’t understand the following answer, do I need to re-install MySQL or can I modify it? I would like to get all of them installed and be able to use mysqli functionality.

I have followed all of the different install proceedures and still can not get it to worl.

A. You need to install MySQL module for php4 or php5. Without php-mysql shared object or module, php scripts won’t able to connect to MySQL database server.

Reply

3 rishi February 2, 2009 at 7:12 pm

i have my own domain and webserver i cant understand that why connection cannot be made to database of mysql with php

Reply

4 jayendra July 5, 2009 at 8:26 pm

I am using wamp server and netbeans ide to write php. I am new to php development. I am not able to connect mysql to php. I don’t know what is happening? I am not able to insert data into mysql.

Reply

5 viji November 13, 2009 at 5:49 am

why this error message in happens:
Could not connect: Access denied for user ‘sss’@'localhost’ (using password: YES)

Reply

6 farwakhan February 24, 2014 at 2:27 am

you shuold be check in include folder confih.php.file and match the db name are the db name is match which db you create in sql like this
$dbserver=”localhost”;
$dbname=”";
$dbusername=”";
$dbpassword=”";
here db name is matched that db name which you create in server

Reply

7 kuldeep singh December 3, 2009 at 6:05 pm

hi i m new user to ubuntu.i could not connect to mysql server through php.
i have installed php5 ,mysql server ,apache server.
somebody can help me?????

Reply

8 Temitope April 19, 2010 at 11:58 am

pls what is wrong with this code, i want to create a mysql table from php in that the user has already specified the table name ,field lenght etc
but rthe code refused to connect to mysql datase, hence didnt produce the result i want!
Apache 2.2.11
Php 5.3.0
mysql 5.1.36
and im using windows xp as my operating system

The Code:

Create A Database Table:STep 3

Adding table

<?php
//create connection
$username="reborn";
$password="reborn";
$hostname="localhost";
$connection = mysql_connect($hostname, $username, $password)
or die("Cudn't Connect To Server");
echo "Connected To MySQL";
$sql="CREATE TABLE $table_name (";
for($i=0;$i < count($field_name);$i++)
{
$sql.= "$field_name[$i] $field_type[$i]";
if($field_length[$i] !=" ")
{
$sql.= "($field_length[$i]),";
}
else{
$sql .=",";
}
}
$sql=sbstr($sql,0,-1);
$sql .=")";
//select database
$db = mysql_select_db("reborndb",$connection)
or die ("Cudn't select database");
//execute SQL query and get result!
$sql_result=mysql_query($sql,$connection)
or die ("Cudn't execute query.");
if(!$sql_result)
{
	echo"Cudn't Create Table!";
} else{
	echo"$table_name has created!";
}
?>

Reply

9 saeed July 11, 2010 at 6:56 am

I have IIS Web server and MySQL configured. Everything works fine except I am not able to connect to a MySQL database server by PHP (I am able to connect mysql server with mysql client and with Java but not with PHP). So how do I configure or connect PHP scripts?

Reply

10 sacha August 29, 2012 at 12:46 am

hi I’m having a problem iv got a MySQL server hosted by Xeround and iv go a login/register widget on my site my host is ueuo.com (only temporary) and when i go register it returns an error saying:

(Database Login failed! Please make sure that the DB login credentials provided are correct
mysqlerror:Can’t connect to MySQL server on ‘instance25245.db.xeround.com’ (4)
Database login failed!)

iv filled out my info correctly and i only have FTP access. Email:sachaw100@hotmail.com if u have an answer please email me.

Reply

11 ajay February 21, 2013 at 7:25 am

mysql connet in php

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: