PHP not connecting to a MySQL database server

Posted on in Categories , , , , , , last updated November 10, 2007

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

12 comment

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

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

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

  3. 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!";
    }
    ?>
    
    
  4. 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?

  5. 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:[email protected] if u have an answer please email me.

Leave a Comment