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
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 11 comments… read them below or add one }
hi i am trying to connect to my sql with this code
but i get an erorr of exceeding the time or long period more than 60 sec
would u help me
Begin Quote: but i get an erorr of exceeding the time or long period more than 60 sec. End Quote.
You need to edit php.ini
Other than displaying the value twice, this worked great for me! Thanks
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?
Thanks! Worked perfectly.
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
Hi I already figured it out. I got the error because the host is set to localhost and not my local ip..
Cheers
Thank you for this.
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!
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 :)
edit to previous post :
$link = mysql_connect(“localhost”, “USERNAME”, “PASSWORD”);
sorry