Posted By:
7 Mar, 2014 11:55 am PST

How to display data on web page from database in PHP?

 

1)                How to display data on web page from database?

 

Once you make connection with database select query is used to retrieve data from the database.

 

Syntax:-

Select all/distinct column name(s)

from table_name;

 

mysql_query () method is used to execute the select query. This method after executing select query returns resultset.

The returned resultset passed to mysql_fetch_array() to retrieve each individual record .

 

Syntax:-

mysql_fetch_array(resultset_resource);

 

resultset_resource= this is result set returned from mysql_query() method.

This method return array, one after another , contain each individual record, till no other record left.

 

Example // display all data present in the employee database

<?php

echo "<h3 >";

echo " Employee Database <br >";

 

require_once 'Connection .php ';

 

$db = getConnection ();// making connection to database

if( $db )

 

{

echo " Database connceted Successfully ";

 

}

 

else

 

{

 

echo " Database not connceted Successfully ";

 

}

$result = mysql_query (" Select * from employee ");

// executing select query

echo "<pre >";

 

while ( $row = mysql_fetch_array ( $result ))// fetching row one-by one

{

foreach ( $row as $data )

 

{

 

echo " { $data } ";

}

 

echo "<br >";

 

}

?>

SelectQuery.php

 

 

 

 
 

<?php

function getConnection ()

 

{

$Connect = mysql_connect (" localhost ", " root ", "");

// connects to the localhost

 

$db = mysql_select_db (" Company ", $Connect );

// connects to the specified database ;

return $db ;

}

?>

Connection.php

 

 

 


SelectQueryResult

 

 

 

 

 

Attachment: SelectQuery.php


It shows how to display data from Database to web page.

Attachment: Connection.php
Was it useful?  




Please login to reply to this problem.



Related Problems on Fixoncloud