Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,478 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




mySQL & PHP: A Beginners Look

 
Reply to this topicStart new topic

> mySQL & PHP: A Beginners Look

skyhawk133
Group Icon



post 3 Dec, 2004 - 03:51 PM
Post #1


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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

Artanis
*



post 28 Jul, 2006 - 04:50 PM
Post #2
oki, this looks great!, going to read this tutorial now smile.gif
Go to the top of the page
+Quote Post

chrismoney
*



post 30 Aug, 2006 - 11:45 PM
Post #3
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
Go to the top of the page
+Quote Post

Demon
*



post 5 Feb, 2007 - 09:33 PM
Post #4
this is a very nice tutorial i am goin to try to make some if i have time with college and work tongue.gif
Go to the top of the page
+Quote Post

maggie_noodles
****



post 5 Mar, 2007 - 06:01 AM
Post #5
VERY VERY helpful! icon_up.gif
Go to the top of the page
+Quote Post

thepcpro
*



post 11 May, 2007 - 02:10 PM
Post #6
QUOTE(maggie_noodles @ 5 Mar, 2007 - 07:01 AM) *

Didn't work for me
icon_up.gif

Go to the top of the page
+Quote Post

fuzzyjello
*



post 24 Jul, 2007 - 10:23 AM
Post #7
Thanks! This was very helpful.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 12/2/08 06:16PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month