Retrieving record from database In PHP

0
7
I assume that you have read tutorial of adding data into tables. I will try my best to keep it as easy as I can.
 
Functions we will use:
 
mysql_connect();
mysql_select_db();
mysql_query() ;
mysql_fetch_assoc();
mysql_num_rows();
 
Ok – now I assume that you have read the above tutorial I mentioned in which we inserted data into table name as test_tbl. Now suppose we have a table named as “test_tbl” and it has the following fields.
 
id, name and address, email.
 
and we have N members registered (means we have N rows, where we N is the number of rows/records in the table ) here they are (the data in test_tbl table) so these are the total values inserted in table “test_tbl”. Now I will make a PHP file mysql_select.php which will retrieve data from test_tbl table.
 
<HTML>

<HEAD>
<TITLE>PHP Tutorial : Working with MySQL</TITLE>
</HEAD>

<BODY>
<?php
// Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$data = mysql_query("SELECT * FROM test_tbl")
or die(mysql_error());
print "<table border cellpadding=3>";

while($info = mysql_fetch_array( $data ))
{
	print "<tr>";
	print "<th>Name:</th> <td>".$info['name'] . "</td> ";
	print "<th>Address:</th> <td>".$info['address'] . " </td></tr>";
	print “<th>” Email ID </th><td>
<a href=”mailto:”.$info[‘email’].”>”.$info[‘email’].”</a>”;
}
print "</table>";
?>

</BODY></HTML>
 
What is mysql_num_rows?
 
This function returns the number of rows effected by SELECT statement. You can use different clauses in SELECT statement, for exmaple you retrieve the name of member whose ID is 2. which is Aman Kumar, so you will use query like this. If there is not data in table it will diplay the error “There is no data in the table”..
 
$res=mysql_query(“SELECT name FROM test_tbl WHERE id=2”);
 
now we have used WHERE clause in the above statement.
 
What is mysql_fetch_assoc() ?
 
This function fetches a a result row as associatve array, and returns an associatve array. In other meaning it fetches the result from the row. In the for() loop I have displayed the fetched result. So the output of this file will be
 
img
 
Retrieving data with mysql_fetch_array
 
Syntax:
 
array mysql_fetch_array ( resource result [, int result_type ] ) Returns an array that corresponds to the fetched row, or FALSE if there are no more rows.
 
mysql_fetch_array() is an extended version of mysql_fetch_row(). In addition to storing the data in the numeric indices of the result array, it also stores the data in associative indices, using the field names as keys.
 
<?php
class dbUtils{

	public function __construct(){
	
	}
	
	public function connect(){
		[email protected]_connect("localhost","root","");
		if(!$con)
			return NULL;
		else 
			return $con;		
	}
	public function createDB($db_to_create){		
	}
	public function createTable($db_to_use,$tbl_name,$command){
	}
	public function insert_record($db_to_use, $command){
	}
	public function update_record($db_to_use, $commnad){
	}
	public function delete_record($db_to_use, $commnad){
	}
	
	public function show_record_array($db_to_use,$table){
		$con=$this->connect();
		$sql="SELECT * FROM ".$table;#"
		echo $sql;
	    mysql_select_db($db_to_use);
	    $result = mysql_query($sql) or die(mysql_error());

	    while($row = mysql_fetch_array($result)) {

	        foreach($row as $key=>$value) {
	            echo " $key= $value     \n";
	        }
			echo "<br />";
	    }
	    mysql_free_result($result);
	    mysql_close();  
	}   
}
?>
<html>
<head>
<title>PHP Tutorial: Working with MySQL[MySQL Select]</title>
</head>

<body>

<form action="<?php echo $PHP_SELF;?>"
 method="post" name="form1">
  Enter database Name<input type="text" 
name="db_name" maxlength="10" size="20">

  <br>
  Enter Table Name <input type="text" 
name="table" maxlength="10" size="20">

  <br>
  <input name="submit" 
type="submit" value="Display Records">
</form>
<hr>
<?php
	if(isset($_POST["submit"]))
	{
?>
<div style="border:thin #FF0000 
solid; width:90%; height:auto">

<?php
$db=$_POST["db_name"];
$table=$_POST["table"];
$dbutil=new dbUtils();
echo $dbutil->show_record_array($db,$table);
echo "</div>";
}
?>


</body>
</html>
 
On Executing above script produces the following output:
 
img