MySQL + PHP Function
Hi All DIC Heads I Saw There Was Many Question About MySQL and PHP Functions, And Decided To Start a Tutorial On This Topic, Before Reading This Tutorial Please Read About MySQL
MySQLRequirements for This Tutorial:
1. Wamp2.
2. Apache 2.2.11 (include in Wamp2)
3. PHP 5.2.9-2 (include in Wamp2)
4. MySQL 5.1.33 (include in Wamp2)
There are About 60 Functions of MySQL Which Uses in PHP 5+
This is a Sample Example How to Connect with MySQL Database, Run a Query, Show a Data from a Query and Then Disconnect from Database.
CODE
<?php
$Con = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')or die('Connection Faild: ' . mysql_error());
echo 'Connected successfully';
$Db = mysql_select_db('pms',$Con) or die('Could Not Select Database');
$Query = mysql_query('SELECT * FROM TableName') or die('Error Executing Query: ' . mysql_error());
echo "<table><tr>";
while ($Result = mysql_fetch_assoc($Query))
{
echo "<td>";
echo $Result['FieldName'];
echo "</td>";
}
echo "</table></tr>";
mysql_close($Con);
?>
In These Examples I’m Using a Database by Name of ‘testingdb’
Lets Start:
First Let’s Talk About Connection of MySQL and PHP
m
mysql_connect();The Function mysql_connect Allow Us to Connect with MySQL, this Function Have 5 Parameters (hostname, username, password, new_link, falg) if username has Password then 3 Parameters are Necessary else 2 Parameters are Necessary.
hostname: default hostname is localhost.
username: default Username is roo.t
password: by default there is no Password we use "" or ''.
new: it is creating a new mysql_connect() function;
flags:
read hereExample:
CODE
$hostname = "localhost";
$username = "root";
$password = "";
mysql_connect($hostname,$username,$password);
mysql_select_db();after Connecting with MySQL we can select database by mysql_select_db(); function.
this function has 2 Parameters (database name, connection);
database name is Necessary, when we are using more then one connection then we are using connection parameter.
CODE
$hostname = "localhost";
$username = "root";
$password = "";
$con = mysql_connect($hostname,$username,$password);
mysql_select_db(‘testingdb’,$con);
Tip:
mysql_error(); When we are using MySQL with PHP and we have an error we can use this function to know or show the Exact error.
Example:
CODE
mysql_connect("localhost","root","******")or print(mysql_error());
mysql_query():this function Execute a SQL Query or Send one Query to MySQL (we can not send more then one query at a time)
to about SQL Query Please Visit MySQL Website
Example:
CODE
mysql_query("SELECT * FROM tbltest")or die(mysql_error());
mysql_num_rows();Get Number of Executed Query and Return a Integer Value, this function is valid for INSERT or SHOW Query only.
This function has one parameter (result)
Example:
CODE
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
$num = mysql_num_rows($Query)or die(mysql_error());
echo $num; //output should be number of rows.
2nd Example:
CODE
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
$num = mysql_num_rows($Query)or die(mysql_error());
if($num>0)
{
echo $num;
}
else
{
echo "Sorry No Record Found in Table";
}
mysql_fetch_assoc();This function is used to Get data from a table by Executing a Query and Return an array().
Example:
CODE
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
$Result = mysql_fetch_assoc($Query);
print($Result[name]);
2nd Example:
in this example we are showing all data of table using html tags.
CODE
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
echo "<table><tr>";
while ($result = mysql_fetch_assoc($Query))
{
echo "<td>";
echo $Result['FieldName'];
echo "</td>";
}
echo "</table></tr>";
or
CODE
<?php
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
?>
<table>
<?php
while ($result = mysql_fetch_assoc($Query))
{
?>
<tr>
<td><?php echo $result[‘id’]?></td>
<td><?php echo $result[‘name’]?></td>
<td><?php echo $result[‘job’]?></td>
</tr>
<?php
}
?>
</table>
mysql_close();We can close a MySQL connection by this function.
Example:
CODE
<?php
$hostname = 'localhost';
$username = 'root';
$password = '';
$Con =mysql_connect($hostname,$username,$password)or die(mysql_error());
$db = mysql_select_db('testingdb')or print(mysql_error());
$Query = mysql_query("SELECT * FROM tbltest")or die(mysql_error());
?>
<table>
<?php
while ($result = mysql_fetch_assoc($Query))
{
?>
<tr>
<td><?php echo $result['id']?></td>
<td><?php echo $result['name']?></td>
<td><?php echo $result['job']?></td>
</tr>
<?php
}
mysql_close($Con);
?>
</table>
thanks for reading.