Information Computer

Saturday, August 13, 2011

Connect PHP With MySQL

The previous post I've described a little about MySQL functions in PHP. Maybe in your previous posts are still confused. Because I was just giving an explanation without an example.Following I will give an example of using the mysql functions in php. So, we will discuss examples of how to connect PHP with MySQL.

The first step we create a file named config.php which serves to connect a PHP file with MySQL.

config.php

<?php
mysql_connect("localhost","root","");
mysql_select_db("data");
?>

note :
"localhost" = hostname MySQL
"root"         = username MySQL
""                = password MySQL (password is not filled)
"data"         = database name

This is the default if you use XAMPP for local web server. Customize to your web server. Next create a file named index.php.

index.php

<?php
include('config.php');
$query= mysql_query("select * from table_data");
$rows = mysql_num_rows($query);
echo "Number of rows = $rows <p>";
while ($result = mysql_fetch_array($query)){
    echo "$result[0]<br>";
}
?>

note :
table_name = table name used (adjust yours).

if successful will appear as shown below




Connect PHP WIth MySQL


I hope this post helpful to us all. sorry if there is one word or a typo. sorry if things go wrong. ask if anyone on this please post a comment below. thank:)

Related Posts



0 comments:

Followers