View Single Post
  #4 (permalink)  
Old 04-01-2008, 07:47 PM
altoyes altoyes is offline
Newbie
 
Join Date: Apr 2008
Posts: 2
Credits: 0
Rep Power: 0
altoyes is on a distinguished road
Default Re: Tutorial: PHP to MySQL

this code looks good, i think it is what i need. i have followed your previous tutorial, but i did not know how to use the console.... i do not know about the binary settings ...


Quote:
Originally Posted by Jordan View Post
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.
Reply With Quote