Establish Connection Between PHP and MySQL


It is possible to execute various commands of MySQL from PHP application. PHP provides various built in functions that allow you to use MySQL commands from PHP page. Thus you can integrate PHP with MySQL.
Following are the various functions of PHP that allows you the facility of integrating PHP with MySQL:

mysql_connect ()


mysql_connect() function allows you to establish connection of PHP application with MySQL server.
Syntax:
$ VariableName = mysql_connect (ServerName, UserName, Password)
Here,
ServerName:
Indicates the name of the MySQL Server with which you want to establish connection.
UserName: Indicates name of the user using which you can logs on to MySQL Server.
Password: Indicates password of the user using which you can logs on to MySQL Server.
This Function returns a Boolean value TRUE or FALSE. If connection establish successfully with MySQL Server then this function returns true value otherwise it returns false.

Example:
<?php
$con = mysql_connect ("localhost","root");
if ($con)
{
echo "Connected with MySQL";
}
else
{
echo "Can not connect with MySQL";
}
?>

     

mysql_selectdb()


mysql_selectdb() function allows you to select database from the list of MySQL server database for performing various operations.
Syntax:
mysql_selectdb (“DatabaseName”,ConnectionName);
Here,
DatabaseName:
Indicates the name of the database that you want to select.
ConnectionName: Indicates the name of the variable that is used at the time of establish connection with MySQL server using mysql_connect () function.
This function returns a Boolean value TRUE or FALSE. If function successfully executed then it returns TRUE otherwise it returns false.

Example:
<?php
$con = mysql_connect("localhost","root");
$db = mysql_selectdb("MyDatabase",$con);
if ($db)
{
echo "Database Selected Succesfully";
}
else
{
echo "Error while selecting the Database";
}
?>

mysql_query()


mysql_query() function allows you to specify and execute the MySQL command on MySQL Server.
Syntax:
mysql_query ("Query");
Here,
Query:
Indicates the MySQL command to be executed.

Example:

<?php
$con = mysql_connect("localhost","root");
$db = mysql_selectdb("MyDatabase");
$cmd = mysql_query("create table Test(ID integer, Name varchar(20))");
if ($cmd)
{
echo "Table created Succesfully";
}
else
{
echo "Error while Executing query";
}
?>

mysql_fetch_row()


mysql_fetch_row() function allows you to retrieve a record from the record set that is returned from executing the MySQL query.
The record that is returned by this function is in the form of numeric array. Numeric array contains index and value associated with that index.
If there is no record in the record set then it returns false value.
Syntax:
mysql_fetch_row ($QueryResult);
Here,
QueryResult
indicates the record set that is returned from executing the MySQL command using mysql_query () function.

Example:
<?php
$con = mysql_connect("localhost","root");
mysql_select_db("mydatabase", $con);
$sql = "select * from Package_Master";
$result = mysql_query($sql,$con);
$Ans = mysql_fetch_row($result);
print_r($Ans);
mysql_close($con);
?>

mysql_fetch_array()


mysql_fetch_array() function allows you to retrieve a record from the record set that is returned from executing the MySQL query.
The record that is returned by this function is in the form of either numeric array, associative array or both.
If there is no record in the record set then it returns false value.
Syntax:
mysql_fetch_array ($QueryResult, ArrayType);
Here,
QueryResult
indicates the record set that is returned from executing the MySQL command using mysql_query () function.
ArrayType indicates the type of the array to be returned. It can have one of the following values:
(1) MYSQL_ASSOC: This type of array contains name of the field and the value associated with that field for current record.
(2) MYSQL_NUM: This type of array contains index of the field and the value associated with that index for current record.
(3) MYSQL_BOTH: It is combination of both Associative array and Numeric array. It is the default type to be returned by this function.

Example:

<?php
$con = mysql_connect("localhost","root");
mysql_select_db("mydatabase", $con);
$sql = "select * from Package_Master";
$result = mysql_query($sql,$con);
$Ans = mysql_fetch_array($result,MYSQL_ASSOC);
print_r($Ans);
mysql_close($con);
?>

mysql_num_rows()


mysql_num_rows() function allows you to retrieve number of records available in the record set.
Syntax:
$Number = mysql_num_rows ($QueryResult);
Here,
QueryResult
is the variable that holds the result returned by mysql_query () function.
It returns numeric value which contains number of records available in the record set.

Example:

<?php
$con = mysql_connect("localhost","root");
mysql_select_db("mydatabase", $con);
$sql = "select * from Package_Master";
$result = mysql_query($sql,$con);
$records = mysql_num_rows($result);
echo $records." Records";
mysql_close($con);
?>

mysql_error()


mysql_error() allows you to retrieve the description of error that is encountered while executing the script.
If the script contains more then one errors then it will returns error description of the last statement in which error is encountered.
If no error encountered while executing the script then it will returns blank string.
Syntax:
mysql_error ();

Example:

<?php
$con = mysql_connect ("localhost","root");
if ($con)
{
echo "Successfully Connected";
}
else
{
die (mysql_error());
}
?>

mysql_close()


mysql_close() function allows you to close the connection that is established using mysql_connect () function.
Syntax:
mysql_close (ConnectionName)
Here,
ConnectionName:
Indicates the name of the variable that is used at the time of establishing connection with MySQL server using mysql_connect () function.
It returns a Boolean value TRUE or FALSE. If connection closed successfully then it returns TRUE otherwise it returns FALSE.

Example:

<?php
$con = mysql_connect ("localhost","root");
mysql_close ($con);
?>

Download Projects


Download Programs