Introduction to php database connectivity
-
Upload
baabtracom-no-1-supplier-of-quality-freshers -
Category
Technology
-
view
456 -
download
4
Transcript of Introduction to php database connectivity
![Page 1: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/1.jpg)
PHP – Mysql connectivity
![Page 2: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/2.jpg)
How to connect PHP with MySQL
1. Connecting to MySQL Server
• mysql_connect(‘host_name’,’username’,’password’);
2. Selecting Database
• mysql_select_db(‘Database Name’);
3. Query execution
• mysql_query(‚query to be executed‛);
![Page 3: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/3.jpg)
Sample Application
![Page 4: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/4.jpg)
Example Program
<html>
<body>
<form action=‛registration.php‛ method=POST>
<input type=‚text‛ name=‚name‛>
<input type=‚text‛ name=‚email‛>
</form>
</body>
</html>
![Page 5: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/5.jpg)
Registration.php will look like this
<?PHP
$name = $_POST['name'];
$email = $_POST['email'];
mysql_connect("localhost","root","");
mysql_select_db("test2");
mysql_query ("insert into tbl_user(vchr_name,vchr_email) values ('$name','$email')");
echo "succesfully inserted‚;
?>
![Page 6: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/6.jpg)
Retrieving data from mysql
• There are 4 ways we can retrieve data from mysql tables
Mysql_fetch_array()
Mysql_fetch_row()
Mysql_fetch_assoc()
Mysql_fetch_object()
• All of the above will return one row from table at a time and then
the next row and so on . So usually we use these functions along
with a while loop
![Page 7: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/7.jpg)
Retrieving data from mysql – mysql_fetch_assoc()
$query = mysql_query (‚select * from tbl_customer");
while($fetch=mysql_fetch_assoc($query))
{
echo $fetch*‘name’+;
echo $fetch*‘vchr_email’+;
}
![Page 8: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/8.jpg)
Retrieving data from mysql – mysql_fetch_row()
$query = mysql_query (‚select * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch[0]; //prints first column the retrieved row
echo $fetch[1]; //prints second column the retrieved row
}
![Page 9: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/9.jpg)
Retrieving data from mysql – mysql_fetch_array()
$query = mysql_query (‚select * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch[0]; //prints first column the retrieved row. Ie name
echo $fetch[1]; //prints second column the retrieved row. Ie email
echo $fetch*‘name’+; //prints first column the retrieved row
echo $fetch*‘email’+; //prints second column the retrieved row
}
‚Mysql_fetch_array() is a combination of mysql_fetch_assoc() and mysql_fetch_row()‛
![Page 10: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/10.jpg)
Retrieving data from mysql – mysql_fetch_object()
$query = mysql_query (‚select * from tbl_customer");
while($fetch=mysql_fetch_array($query))
{
echo $fetch->name; //prints first column the retrieved row
echo $fetch->email; //prints second column the retrieved row
}
‚Returns an object with properties that correspond to the fetched row and moves the
internal data pointer ahead.‛
![Page 11: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/11.jpg)
Mysql_num_rows()
• PHP provides another utility inbuilt function mysql_num_rows()
which will return the number of rows returned from the executed
query
– Example
$query = mysql_query (‚select * from tbl_customer");
$num=mysql_num_rows($query);
if($num>0)
{
while($fetch=mysql_fetch_array($query)
{ .................. }
![Page 12: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/12.jpg)
Questions?
‚A good question deserve a good grade…‛
![Page 13: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/13.jpg)
End of day
![Page 14: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/14.jpg)
If this presentation helped you, please visit our page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
![Page 15: Introduction to php database connectivity](https://reader034.fdocuments.us/reader034/viewer/2022052323/55852cdbd8b42a30308b4a27/html5/thumbnails/15.jpg)
Contact Us
Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550
Start up VillageEranakulam,Kerala, India.
Email: [email protected]