PHPsu
MANUAL ZH  |  EN
     


Current Position :| index>Beginners> mySQL & PHP: A Beginners Look

mySQL & PHP: A Beginners Look

FROM: AUTHOR: TIME:2008-08-16 HITS:


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
view plainprint?
  1. <?  
  2. // MySQL Connection Information  
  3. $server = "localhost";                // The name of your server  
  4. $username = "username";          // The MySQL username  
  5. $password = "password";         // The MySQL password  
  6. $dbname = "database_name";               // The name of the database  
  7. ?>  


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
view plainprint?
  1. <?          
  2. // mySQL Connect String  
  3. $connection = mysql_connect("$server","$username","$password");  
  4.   
  5. // Select the database  
  6. mysql_select_db("$dbname");  
  7. ?>  


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
view plainprint?
  1. <?  
  2. $sql = "SELECT id, username, email, posts FROM members;";  
  3.   
  4. // execute SQL query and get result  
  5. $sql_result = mysql_query($sql,$connection)  
  6.      or die(mysql_error());   
  7. ?>  


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
view plainprint?
  1. <?  
  2. // Loop through the data set and extract each row in to it's own variable set  
  3.   
  4. while ($row = mysql_fetch_array($sql_result)) {  
  5.      extract($row);  
  6. ?>  


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
view plainprint?
  1. <?  
  2. // Loop through the data set and extract each row in to it's own variable set  
  3.   
  4. while ($row = mysql_fetch_array($sql_result)) {  
  5.      extract($row);  
  6. ?>  
  7.     <li>Username: <? Echo $username; ?> (<? Echo $email; ?>)  
  8. <?  
  9. // End while loop  
  10. }  
  11. ?>  

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
view plainprint?
  1. 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.
TITLE:mySQL & PHP: A Beginners Look
Previous:PHP : Beginners Guide PT.3
Next:None
Copyright 2008 The PHPsu All rights reserved. This mirror generously provided by: .Hp Inc.
Last updated: Sat Aug 16 21:55:08 GMT-8 2008