About Linux FAQ

Browse More FAQs:

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

Posted by Vivek Gite [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

E-mail this to a friend      Printable version

Related Other Helpful FAQs:

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.