Page 1 of 1

All Functions of PHP+MySQL Rate Topic: ***** 2 Votes

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Posted 15 May 2009 - 10:25 PM

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 MySQL

Requirements 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.
<?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 here
Example:
$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.
$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:
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:
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:
$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:
$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:
$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.
$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
<?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:
<?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.

Is This A Good Question/Topic? 2
  • +

Replies To: All Functions of PHP+MySQL

#2 sfw  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 27
  • Joined: 24-April 08

Posted 11 March 2010 - 04:14 PM

great tutorial. very helpful to my current task...thank you. can you continue on the same theme and discuss how to filter results based on user inputs/selections from text fields and dropdown menus.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1