Monday 25 April 2016

Select a database and Perform a Operation On Database In PHP

=>We can Use a mysql_select_db() method for select a database.


Using mysql_select_db() method u can select a Database in PHP.



Syntax

mysql_select_db(database_name, connection_object);

Example

<?PHP
$cn=mysql_connect(“localhost”,”root”,””);
mysql_select_db(“databasename”,$cn);

?>

=> We can use a mysql_query() method for perform operation in database.

=> Query Fire on Database

Using mysql_query() method you can perform operation on Database in PHP. Main four query perform on database Insert, Update, Delete and select. 
you can perporm insert, update and delete operation on database at that time mysql_query() method return a index array and you can perform a Select operation on database at that time mysql_query() method return a Associative array.

Syntax

mysql_query(connection_object, query);

Example

<?PHP
$cn=mysql_connect(“localhost”,”root”,””);
$que=”select * from student”;
$result=mysql_query($cn,$que);

?>


=> We can use a mysql_fetch_row() & mysql_fetch_array()  method use for fetching a record.

Using mysql_fetch_row() method fetch a record from a record set as a numeric array.
This method use for fetching a row from database and access in the script.
Mysql_fetch_row() method store a data like Index Array.
Mysql_fetch_row() method return a Index Array.

Syntax

mysql_fetch_row(query_method_result);

Mysql_fetch_array() method work same as a mysql_fetch_row().
But mysql_fetch_array() method store a data like Associative Array.
mysql_fetch_array() method return a Associative Array.

Syntax

mysql_fetch_array(query_method_result);

Example of mysql_fetch_row()

<?PHP
$cn=mysql_connect(“localhost”,”root”,””);
$result=mysql_query($cn,“Select * from Student”);
If($rows= mysql_fetch_row($result))
{
echo $rows['0’];
  echo "<br />";
}
?>

Example of mysql_fetch_array

<?PHP
$cn=mysql_connect(“localhost”,”root”,””);
$result=mysql_query($cn,“Select * from Student”);
If($rows= mysql_fetch_array($result))
{
echo $rows['name’];
  echo "<br />";
}

?>


No comments:

Post a Comment