PHP and MySQL Connectivity

We know that there are lots of tutorials (some official too) on the internet about PHP and MySQL connectivity. Then why am i posting it here ? i am posting because most of those articles are not simple and easy to understand and this is why we are doing it here. We assume that you have already installed XAMPP in your System and running it using XAMPP control panel.

To connect PHP to MySQL database you need to know following important things:

  1. Host name.
  2. MySQL user name.
  3. MySQL password.

If you have installed XAMPP in your system (not web server) then host name is localhost. By default MySQL user name and password is “root” and blank ( “” ) respectively. Let us create one simple project and try to connect our PHP code to MySQL.

If you are on Windows then there is “htdocs” folder in “C:/xampp/htdocs/” (if installed on default location). If you are on Linux (most probably Ubuntu) then it is located on “/opt/lampp/htdocs” (you should switch to root user before creating folder in it.).

In any case create any folder, lets say “test-db-connection” and create simple PHP file with following code.

$host="localhost";
$user="root";
$password="";
$con=mysql_connect($host,$user,$password);
if(!$con) {
  echo '<h1>Connected to MySQL</h1>';
} else {
   echo '<h1>MySQL Server is not connected</h1>';
}

Save it in the project folder and go to localhost/test-db-connection/filename.php and see what it is giving.

mysql_connect() is PHP inbuilt function to connect to MySQL database with the parameter shown above.

To perform SQL queries, you need to select database. You can do that using mysql_select_db(“database_name”,optional connection variable). Once Database is selected you can perform queries using mysql_query(“SQL query”). Here is sample code.

$host="localhost";
$user="root";
$password="";
$con=mysql_connect($host,$user,$password);
if(!$con) {
  echo '<h1>Connected to MySQL</h1>';
  //if connected then Select Database.
  $db=mysql_select_db("YOUR_DATABASE_NAME",$con);
  $query=mysql_query("YOUR_MYSQL_QUERY",$db);
}
else {
  echo '<h1>MySQL Server is not connected</h1>';
}

I hope you have understood the concept. You can ask any doubt in comment.

Shahid (UnixRoot) Shaikh

Hey there, This is Shahid, an Engineer and Blogger from Bombay. I am also an Author and i wrote a programming book on Sails.js, MVC framework for Node.js.

Related Posts

40 Comments

    1. Yes. Let’s say you are building Registration form where user will submit and their information will be store in Database , in that kind of scenario you need to connect to your database ( assuming MySQL here) in order to do further database operations.

  1. when i took ur code in my project. basic one of just checking whether its connected or not… it kept loading for a long time.. n didnt provide any result.

  2. Your logic is reversed. If !$con that means your connection attempt failed.

    So either write
    if ($con) {
    instead of
    if (!$con) {

    or just handle the failure first, throw an exception and then continue assuming success, like it is done in the official documentation examples here: http://php.net/manual/en/function.mysql-connect.php

    In this particular case, it doesn’t make any difference whatsoever whether you use mysql_connect or mysqli_connect. The former is deprecated and will be removed at some point in the future, but as of PHP 5.5 it still works.

  3. Fatal error: Call to undefined function mysql_connect() in C:\xampp2\htdocs\test\index.php on line 5

    what does it mean?

  4. I took ur code in my project and I got
    “Warning: mysql_select_db() expects parameter 1 to be mysqli, string given in C:\xampp2\htdocs\BDPortal\admindbconf.php on line 15”

    Can you help me with this?

    thanks.

  5. I got same response as tin above from the code given.
    Please help because I have tried several codes and none is working.

  6. $query=mysql_query(“YOUR_MYSQL_QUERY”,$db);

    Above method has error, since the second parameter has to be a connection object not db.

    Please correct.

  7. I did try this procedure on my XAMPP but it not working .
    if anyone could help me please .
    i got this error in my browser.
    Fatal error: Uncaught Error: Call to undefined function mysql_connect() in C:\xampp\htdocs\conn.php:5 Stack trace: #0 {main} thrown in C:\xampp\htdocs\conn.php on line 5

  8. $host=”localhost”;
    $user=”root”;
    $password=””;
    $con=mysql_connect($host,$user,$password);
    if(!$con) {
    echo ‘Connected to MySQL’;
    } else {
    echo ‘MySQL Server is not connected’;

    } anable to connect

  9. I’m very new to PHP, but not programming. The following code from the example would seem to read that if it is NOT connected then echo ‘Connected to MySQL’. Maybe it’s different in PHP, but in other languages the ! sign means NOT. (I’m not shouting with capital letters just trying to be clear)

    —-
    if(!$con) {
    echo ‘Connected to MySQL’;
    } else {
    echo ‘MySQL Server is not connected’;
    }
    —-

    Whatever the case, my code seems to echo “Connected to MySQL” even with a made up username. This is surely a reflection of my inexperience with PHP, but just curious how the ! operator works.

  10. m getting els part msg dt sql server is not connected…..i hv checked the code bt not able to catch the problem…plz help me….:)

  11. Hi,
    I am not able to select database only
    I just wasted more than 15 hours everytime it says connection problem
    Code:
    $con is my connection string
    $con=mysql_connect(“localhost”,”root”,””);
    mysql_select_db(“test”,$con) or die(“connection problem”);

    Output is connection problem

  12. how to run in xampp server
    i was created a database with name my_db and it was saved in xampp ht docs folder.
    how can i run it.

  13. hi when i run by xampp the filename.php i have this output:
    $host=”localhost”; $user=”root”; $password=””; $con=mysql_connect($host,$user,$password); if(!$con) { echo ‘
    Connected to MySQL

    ‘; } else { echo ‘
    MySQL Server is not connected

    ‘; }

    BUT when i run the second code replacing the name of my database and query i have error:
    Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.

    Error 404

    localhost
    Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/5.6.28

    How can it be solve? I’m new and i need to understnd it

Leave a Reply

Your email address will not be published. Required fields are marked *