Full Version: mySQL & PHP: A Beginners Look
Dream.In.Code > Programming Tutorials > PHP Tutorials
skyhawk133
mySQL & PHP: A Beginners Look

As in many languages, there are 150 different ways to do just about everything, the same rule applies for a simple connection and output of data in a mySQL database using PHP. This tutorial will give you only one way of accomplishing this, and while it works, it is definitely not the only way.

We'll assume you already have some sort of database setup, whether it's from a pre-installed script, or a database of your own creation, it doesn't matter. It should at least have a few records in it for testing purposes.

For the sake of this tutorial, we will place all of the code in one file, however, it is good practice to place the connection variables and connection string in a separate config type file which can be included each time you make the database connection.

So lets get started, first we should define out server, username, password, and the database we will be connecting to. The easiest way to do this is with a few variables:

php
<?
// MySQL Connection Information
$server = "localhost";                // The name of your server
$username = "username";          // The MySQL username
$password = "password";         // The MySQL password
$dbname = "database_name";               // The name of the database
?>


Once we have our connection variables, we can go ahead and make the physical connection to the database server and select the database to use:

php
<?        
// mySQL Connect String
$connection = mysql_connect("$server","$username","$password");

// Select the database
mysql_select_db("$dbname");
?>


Alrighty, so now we've got an open connection to the database, from this point we can pass SQL commands. For this tutorial we'll just be using a SELECT statement. However, this is where you would put your INSERT, UPDATE, or DELETE statement depending on what you were trying to accomplish.

php
<?
$sql = "SELECT id, username, email, posts FROM members;";

// execute SQL query and get result
$sql_result = mysql_query($sql,$connection)
    or die(mysql_error());
?>


This will result in all records from the 'members' table being retrieved and stored in the $sql_result variable. Since we've selected the id, username, and email columns, we will need to pull them back out in to a usable form. This is done using the mysql_fetch_array function:

php
<?
// Loop through the data set and extract each row in to it's own variable set

while ($row = mysql_fetch_array($sql_result)) {
    extract($row);
?>


This is the beginning of the loop which will pull each row from the $sql_result variable. Each time the while loop runs, it goes to the next row. You will notice we are using extract($row) in the above code, what does this do you ask? In most tutorials you read, data in each column is referenced like this: $row['username'], well, that's one to many keys to press, so by using extract(), we are able to pull each column in to it's own variable. So, instead of $row['username'], we use $username to reference data in the username column. Now, let's take the piece of code above, and make it actually show us something:

php
<?
// Loop through the data set and extract each row in to it's own variable set

while ($row = mysql_fetch_array($sql_result)) {
    extract($row);
?>
   <li>Username: <? Echo $username; ?> (<? Echo $email; ?>)
<?
// End while loop
}
?>

So what exactly did we just do? We looped through the entire dataset and each time we made a bullet with the username and the email address in parentheses:

QUOTE
Username (email@domain.com)
Username2 (email2@domain.com)
Username3 (email3@domain.com)


You'll notice that instead of using one big echo, like this:

php
Echo ("Username: $username ($email)");


We instead opted to close our PHP tag and write basic HTML with nested <? Echo $variable; ?> statements inside. This makes reading the code a little easier, and also makes the code process faster because only code in between the <? And ?> is sent to PHP.

So that's it, you've connected to the database, executed a SQL statement, retrieved the records, and displayed them using the actual column name as the variable.
Artanis
oki, this looks great!, going to read this tutorial now smile.gif
chrismoney
Thanks for the tutorial, I am reading it and now I am starting to playing with mySQL and PHP because I am doing that also in my class. YOU ARE THE BEST.


CHRISMONEY
Demon
this is a very nice tutorial i am goin to try to make some if i have time with college and work tongue.gif
maggie_noodles
VERY VERY helpful! icon_up.gif
thepcpro
QUOTE(maggie_noodles @ 5 Mar, 2007 - 07:01 AM) *

Didn't work for me
icon_up.gif

fuzzyjello
Thanks! This was very helpful.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.