How to: Connect to my MySQL Database server using command line and php

Posted on in Categories , , last updated May 31, 2007

Q. How do I connect to my MySQL database server using command line (over ssh) or using PHP?

A. You can connect to MySQL database server using mysql command line client or using programming language such as PHP or perl.

Task: Use command line client – mysql

Genral syntax is as follows:
mysql -u DBUSER -h DBSERVERNAME -p

So at a shell prompt you type all one single line to connect to database server install on localhost for vivek user:
$ mysql -u vivek -h localhost -p

Supply the password when prompted for password. Make sure you replace vivek and localhost name with your database username and hostname.

Task: Use PHP to connect to MySQL

Type the following PHP code:

<?php
   $link = mysql_connect("localhost", "USERNAME", "PASSWORD");
   mysql_select_db("DATABASE");

   $query = "SELECT * FROM TABLE";
   $result = mysql_query($query);

   while ($line = mysql_fetch_array($result))
   {
      foreach ($line as $value)
       {
         print "$value\n";
      }
   }

    mysql_close($link);
?>

Make sure you replace USERNAME and PASSWORD with your database user name and password. Also, replace TABLE and DATABASE with the valid table and database names from your database.

Upload PHP file and type url http://yourdomain.com/myscript.php

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

Share this on (or read 16 comments/add one below):

16 comment

    1. Hi sr,
      with the age of this post i guess you would have solved this by now but for others:

      looking at the code in the example ‘select * from table’ and a loop printing out each value on the screen theres a good chance that the script is taking too long to execute (default is 60 seconds) for others wanting to test connections i’d suggest putting on a limit eg.
      SELECT * FROM table LIMIT 50;
      Then that will limit mysql to only pull the first 50 rows

  1. i didn’t get the solution of my problem. actually i’ve created database on my own system and i want to connect it to my web host server so that every body who visits my site can access data from that database. is it possible? what could i have to do for that?

  2. Hi thanks for your code it really works. But i got an error when i try to use my own ip.
    The error says, “Host ‘comp.local’ is not allowed to connect to this MySQL server”. Any ideas?

    Thanks in advance

  3. Thank you soo much for this absolute life saver! i’m currently converting our old site to PHP (after using HTML with CSS for so long) it got abit time consuming trying to flick through books which conveniently miss the PRINT function *palms own face*. I can’t believe after hours of chewing through my code trying to decipher the simple way to produce the results it was something as small as that. 10 thumbs up!

  4. In response to feedback ‘3’ of this thread (and for anyone reading this who gets the same result). The only way i could remove the duplicate results after fetching an array query was to take the PRINT out of the code and put it into an ECHO command by adding an extra variable.(i used $range for this example but you can use anything)

    e.g-

    <?php
    $link = mysql_connect("localhost", "USERNAME", "HOST");
    mysql_select_db("DATABASE");
    $query = "SELECT * FROM TABLE";
    $result = mysql_query($query);
    $range = "";
    while ($line=mysql_fetch_array($result))
    {
    foreach ($line as $value)
    {

    //$range .= $value;
    }

    $range .= "$value”;
    mysql_close($link);

    }

    ?>

    Then later in the code to produce the results from the query i put the ECHO in a element:

    not really related to much to the topic and i know there might be other easier ways but i thought it might help people who are new to PHP like me :)

Leave a Comment