PHP Freelancer

Freelance PHP Developer

Mysql Tutorial – Select/Retrieve data from table

Posted on | March 8, 2009 | No Comments

Hello Friends,

If you are a learner (new) in PHP / Mysql and don’t know how to select / retrieve data from  table using php file than Here is the easiest and error-free way to select / retrieve data from table using PHP.

You just need to execute SELECT query with table name and you will  get all records from that table . There are many PHP inbuilt functions available to fetch result we will get from SELECT query . Here i have used mysql_fetch_assoc($res) as this is very easy and understandable .

<?php
///*** This file will help you to select data from tables in MYSQL database using PHP – Rakshit Patel **////

include(“mysql-connection.php”);
mysql_select_db(“testing”); // select database
$sql = “SELECT * FROM test_table”;
$res = mysql_query($sql);

while($row = mysql_fetch_assoc($res))
{
echo “Name :”.$row['name']}.” <br>” .
“Email :”.$row['email'].” <br>” .
“Message :”.$row['message'].” <br>”;
}

include(“mysql-connection-close.php”);
?>

Here first i have added mysql-connection.php file which will make connection with Mysql database. If you dont know how to make connection with Mysql database than click here.

Now i have selected Mysql database (named testing ). if you have not created database than click here.

After selecting database , you need to execute the query as written above to select record from table. If you have not created table yet , than click here.

Thats it.

At last i have included mysql-connection-close.php file.

<?php ///*** This file will close MYSQL connection using PHP – Rakshit Patel **////

mysql_close($connection); // close mysql connection
?>

This is nothing but it will close connection which we have made earlier. Its not necessary but a sign of Good Programmer.

Cheers !!

      
Plugin by: PHP Freelancer

Comments

Leave a Reply





Freelance PHP Developer