PHP not connecting to a MySQL database server

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?

ADVERTISEMENTS

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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
12 comments… add one
  • rajesh Dec 20, 2007 @ 16:25

    I m not able to connect localhost. error 3306

  • Bill Brinegar Jan 22, 2009 @ 23:19

    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.

  • rishi Feb 2, 2009 @ 19:12

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

  • jayendra Jul 5, 2009 @ 20:26

    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.

  • viji Nov 13, 2009 @ 5:49

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

    • farwakhan Feb 24, 2014 @ 2:27

      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

  • kuldeep singh Dec 3, 2009 @ 18:05

    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?????

  • Temitope Apr 19, 2010 @ 11:58

    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!";
    }
    ?>
    
    
  • saeed Jul 11, 2010 @ 6:56

    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?

  • sacha Aug 29, 2012 @ 0:46

    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.

  • ajay Feb 21, 2013 @ 7:25

    mysql connet in php

  • aaa Jan 26, 2016 @ 7:49

    Thankssss!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.