Your full service technology partner! 
-Collapse +Expand
Search PHP Group:

-Collapse +Expand PHP Store

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBWebsite Scri...PHP   Print This     
  From the February 2016 Issue of Prestwood eMag
PHP and MySQL Basics
Posted 16 years ago on 2/13/2006 and updated 1/10/2008
Take Away: The basics to get you going in PHP with a MySQL Database.
 Tags: PHP , MySQL


Connecting to a MySQL database in PHP is easy as the functions are built into PHP (in the latest versions of PHP a little bit of configuration is required) the code is below.

$conn = mysql_connect($dbhost, $dbuser, $dbpass)

To explain the code above, $conn is the connection to the database, this variable is used when making queries, etc. the mysql_connect function creates a connection. $dbhost is the location where MySQL is hosted (localhost if it is stored on your local machine). $dbuser and $dbpass are the username and password for a user in MySQL. mysql_select chooses the database to use in the MySQL server.

To run a query

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

To explain the code above, $query is a string containing a SQL statement
mysql_query runs a query and stores the result in the $result variable.

To iterate through the $results:

while($row = mysql_fetch_row($result))

Remember to close your database connection when you are done:



Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P163A1
Enter key:
KB Post Contributed By Adam Lum:

Adam Lum is a part time developer for Prestwood Software and participates in this online community when time allows. His day-to-day work is C# coding but his current intrests (right now) are Ruby on Rails and iOS programming with Objective-C.  He has also coded several projects in Java, C++, ASP Classic, and PHP.  His personal website can be found at

Visit Profile

 KB Article #100303 Counter
Since 4/2/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2022 Prestwood IT Solutions.   [Security & Privacy]