PHP is a popular scripting language used for web development that allows connection to MySQL databases. MySQL is a relational database management system that uses SQL. To connect PHP to MySQL, you use the mysql_connect function to connect to the MySQL server, mysql_select_database to select the database, and mysql_query to execute queries. There are different mysql_fetch functions that can be used in a while loop to retrieve rows of data from the MySQL database into PHP.
2. What is the use of PHP and MySQL?
PHP is the most popular scripting language for web
development. It is free, open source and server-side (the
code is executed on the server). MySQL is a Relational
Database Management System (RDBMS) that uses
Structured Query Language (SQL).
3. How to connect PHP with MySQl?
Step 1: Connecting to MySQl Server
mysql_connect(‘host _name’ , ’username’ , ’password’);
Step 2: Selecting Database
mysql_select_database(‘Database Name’);
Step 3: Query Execution
mysql_query(‘Query to be Executed’);
4. Step 1: Connecting to MySQl Server
mysql_connect(‘host _name’ , ’username’ , ’password’);
Eg: mysql_connect(‘localhost’ , ‘root’ , ’’);
6. Step 3: Query Execution
mysql_query(‘Query to be Executed’);
mysql_query(“Update table CACMS_tbl set institute _name=‘CACMS’ ”);
7. Example Program
<html>
<body>
<form action=“ ” method=Post>
<input type=“text” name=“name”>
<input type=“email” name =“email”>
<input type=“submit” value=“Register”
name=“Button”>
</form>
</body>
</html>
8. Registration.PHP will look like this
<?php
mysql_connect(“localhost”, “root”, “”);
mysql_CACMS_db(“test”);
if(isset($_POST[‘Button’])){
mysql_query(“INSERT INTO tbl_student(sname,email)
values(‘$_POST[name]’, ‘$_POST[email],…”);
echo “Successfully Inserted”;
?>
9. Retrieving Data From MySQl
There are four different ways to fetch the data from
database using PHP.
MySQl _fetch_array()
MySQl _fetch_row()
MySQl _fetch_assoc()
MySQl _fetch_object()
All of above will return one row from table at a time and
then next. So, usually we use these functions along with
while loop.
10. MySQl _fetch_array()
$query=mysql_query(“select * from tbl_cacms”);
while($fetch=mysql_fetch_array($query))
{
echo $fetch[0]; //prints first column the retrieved row
echo $fetch[‘vchr_phone’]; //print second column the
retrieved row
}
11. MySQl _fetch_row()
$query=mysql_query(“select * from tbl_cacms”);
while($fetch=mysql_fetch_row($query))
{
echo $fetch[0]; //prints first column the retrieved row
echo $fetch[1]; //print second column the retrieved
row
}
12. MySQl _fetch_assoc()
$query=mysql_query(“select * from tbl_cacms”);
while($fetch=mysql_fetch_assoc($query))
{
echo $fetch[‘vchr_sname’]; //prints first column the
retrieved row
echo $fetch[‘vchr_phone’]; //print second column the
retrieved row
}
13. MySQl _fetch_object()
$query=mysql_query(“select * from tbl_cacms”);
while($fetch=mysql_fetch_object($query))
{
echo $fetch->’vchr_sname’; //prints first column the
retrieved row
echo $fetch->‘vchr_phone’; //print second column the
retrieved row
}