View Single Post
  #1 (permalink)  
Old 09-22-2006, 06:02 PM
Jordan's Avatar   
Jordan Jordan is online now
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Age: 26
Posts: 5,872
Last Blog:
Performance or Maintai...
Rep Power: 20
Jordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud ofJordan has much to be proud of
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default Tutorial: PHP to MySQL

Creating a connection between PHP and MySQL is fairly simple. PHP provides all of the functions you need to connect and extract data.

Before we begin have a database setup in MySQL and know the username, password and name of the database.

Create a table and add some default values into it. Remember the name of this table.

1) To connect to MySQL you really only need a few variables. To start, open notepad and add these lines:

PHP Code:
<?php
// mysql config
$mysql_username "yourname";
$mysql_password "password";
$mysql_database "db";
$mysql_server "localhost";
These are the values that we will use to connect to MySQL from PHP. Replace all of these values with your MySQL values. $mysql_server = "localhost" can generally be as is.

2) Now we will connect to MySQL using the variables above:

PHP Code:
$link mysql_connect($mysql_server$mysql_username$mysql_password);
mysql_select_db($mysql_database$link); 
As simple as that you have a connection to MySQL.

3) Lets pull some data out of our DB. You should have the table name as stated above and I named mine tbl_tutorial so replace this with your table name. We now need to build a query

PHP Code:
$query "SELECT * FROM tbl_tutorial"
4) Execute the query

PHP Code:
$results mysql_query($query$link); 
This will actually make the query on the MySQL database. Once this has been done all we need to do is fetch the results and manipulate them.

5) Display our results
PHP Code:
// Fetch results
while ($row mysql_fetch_row($results)) {

   
// Data will be displayed in an array $row[0]  
   
print "$row[0]<br>";


The $row array will contain all of the fields in your table starting with 0 and ending with the number of rows - 1.

6) Close our connection

PHP Code:
// Close our connection
mysql_close($link);

?> 
Thats it. I've attached the PHP file from this tutorial if you need to reference it.
Attached Files To view attachments your post count must be 1 or greater. Your post count is 0 momentarily.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
Don't hesitate to ask any questions that you have! Check out our ASCII Calculator!
Reply With Quote

Sponsored Links