Skip to content

TechXcel.com - provides technical excellence...

Narrow screen resolution Wide screen resolution Increase font size Decrease font size Default font size default color orange color green color
You are here: Home arrow PHP arrow PHP Database Integration With MySQL
PHP Database Integration With MySQL PDF Print E-mail
(7 votes)
The Article - php
Written by Chetankumar Akarte   
Wednesday, 07 March 2007
Article Index
PHP Database Integration With MySQL
Create New Database
Select MySQl Database
Adding Data to Table
Retrive Data
Error Handing
Source Code

Retrieve data:

The mysql_query() function of PHP also allow us to get data from table. The following code gives you an idea about that...

$result = mysql_query( "SELECT * FROM $table");
$total_rows = mysql_num_rows( $result );

After perfoming SELECT operation using mysql_query() function, result is stored in the identifier $result. Now with the mysql_num_rows()  function, we get the total number of rows of table ‘visitor’.

Now, to display data of table, we use the PHP function mysql_fetch_row() which gives us all data of the table. mysql_fetch_row() returns false when it finds end-of-data in the table. We use it with the while condition to display the content of table ‘visitor’. Following code displays table content (Code-5).

Code 5: printdata.php
<html>
 <head>
  <title>Server test Page </title>
 </head>
 <body>
  <p>Display table content of MySQL Database.
  <?php
   $link = mysql_connect("mysql_host", "mysql_user", "mysql_password") or die("Could not connect : " . mysql_error());
   print "<p>Connected successfully<p>";
   $DB = "user";
   $table = "visitor";
   mysql_select_db($DB) or die ("Database $DB not select.." . mysql_error());
   print "Successfully select the Database: $DB ";
   $result = mysql_query( "SELECT * FROM $table");
   $total_rows = mysql_num_rows( $result );
   print "<p>There are $total_rows in table : $table </p><p> Table contents are: </p><P>";
   print "<table border=1> \n";
   while ( $pr_row = mysql_fetch_row(  $result ) )
   {
    print "<tr>";
    foreach ( $pr_row as $data )
     print "\t <td>$data</td>";
    print "</td>\n";
   }
   print "</table>\n";
   mysql_close ( $link );
  ?>
 </body>
</html>

After executing Code-5 at my terminal, I got output as in Figure-1. (Please note that I have added data to the table. Here you see only the structure of the output.)
Retrive Data
PHP MySQL Database Integration




Last Updated ( Tuesday, 13 March 2007 )
 

Who's Online

User Login

PayPal Donation

Enter Amount:

Syndicate

TechXcel's Friends

Tutorials Garden
Wicolorz
pixel2life

Text Link Add

Statistics

Members: 276
News: 60
Web Links: 6
Visitors: 348428

Google Seach

Google AdSense

TechXcel Referrals

TechXcel's Sponcer...