+ All Categories
Home > Technology > Introduction to php database connectivity

Introduction to php database connectivity

Date post: 20-Jun-2015
Category:
Upload: baabtracom-no-1-supplier-of-quality-freshers
View: 453 times
Download: 4 times
Share this document with a friend
Popular Tags:
15
PHP Mysql connectivity
Transcript
Page 1: Introduction to php  database connectivity

PHP – Mysql connectivity

Page 2: Introduction to php  database connectivity

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

Sample Application

Page 4: Introduction to php  database connectivity

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

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

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

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

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

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

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

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

Questions?

‚A good question deserve a good grade…‛

Page 13: Introduction to php  database connectivity

End of day

Page 14: Introduction to php  database connectivity

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

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]


Recommended