Directory Submission
WL Marketing - Your #1 link building service

Free Articles » Internet » Web-development » Php » How to use PHP with MySQL on your web site

How to use PHP with MySQL on your web site


Autor: admin :: Views: 573 :: :: View PDF :: Print View

PHP MySQL Tutorial


Connect to MySQL

Interacting with MySQL makes PHP a far more powerful tool. In this tutorial we will go through some of the most common ways PHP interacts with MySQL. To follow along with what we are doing, you will need to create a database table by executing this command:

CREATE TABLE friends (name VARCHAR(30), fav_color VARCHAR(30), fav_food VARCHAR(30), pet VARCHAR(30));
INSERT INTO friends VALUES ( "Rose", "Pink", "Tacos", "Cat" ), ( "Bradley", "Blue", "Potatoes", "Frog" ), ( "Marie", "Black", "Popcorn", "Dog" ), ( "Ann", "Orange", "Soup", "Cat" )


This will create a table for us to work with, that has friends' names, favorite colors, favorite foods, and pets.


The first thing we need to do in our PHP file is connect to the database. We do that using this code:


<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
?>


Of course you will replace server, username, password, and Database_Name with the information relevant to your site. If you are unsure what these values are, contact your hosting provider.


The first thing we need to do in our PHP file is connect to the database. We do that using this code:


<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
?>


Of course you will replace server, username, password, and Database_Name with the information relevant to your site. If you are unsure what these values are, contact your hosting provider.


Retrieve Data

Next we will retrieve the information from the database table we created called "friends"

// Collects data from "friends" table
$data =
mysql_query("SELECT * FROM friends")
or
die(mysql_error());


And we will then temporally put this information into an array to use:


// puts the "friends" info into the $info array
$info =
mysql_fetch_array( $data );


Now let's print out the data to see if it worked:


// Print out the contents of the entry
Print "<b>Name:</b> ".$
info['name'] . " ";
Print "<b>Pet:</b> ".$
info['pet'] . " <br>";


However this will only give us the first entry in our database. In order to retrieve all the information, we need to make this a loop. Here is an example:


while($info = mysql_fetch_array( $data ))
{
Print "<b>Name:</b> ".$
info['name'] . " ";
Print "<b>Pet:</b> ".$
info['pet'] . " <br>";
}


So let's put all the these ideas together to create a nicely formatted table with this final php code:


<?php
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
$data =
mysql_query("SELECT * FROM friends")
or
die(mysql_error());
Print "<table border
cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<
tr>";
Print "<
th>Name:</th> <td>".$info['name'] . "</td> ";
Print "<
th>Pet:</th> <td>".$info['pet'] . " </td></tr>";
}
Print "</table>";
?>

Source: Free Articles

Other Interesting Articles
Pros and Cons of Different Types of Investments
Tips for Choosing High Performance Mutual Fund
Tips for Choosing The Best Stock Broker
ETF Advantages and Disadvantages
Free Slot Machine Games on the Web
BMW Pros and Cons
Six Little Spending Mistakes That Can Cost You Financial Freedom
Want to Live Debt Free? Free Tips That Will Help
Social Bookmarking

About the Author

Angela Bradley is a web designer, programer, and owner of www.Identity.st. She has been creating websites for over 10 years and continues to expand her programing knowledge through projects for herself and her clients.

Comments

No comments posted.
Add Comment

Enter the code shown

Visual CAPTCHA