0% found this document useful (0 votes)
114 views14 pages

PHP Mysql KVR

This document discusses connecting to a MySQL database from PHP. It covers connecting to the database server, selecting a database, performing SQL queries, handling errors, building and running queries, and inserting, updating and deleting data. The document provides code examples for connecting to MySQL, selecting a database, running queries, handling errors, and retrieving and printing result rows. It also discusses combining PHP and HTML forms to submit data to a PHP script for database access.

Uploaded by

satishkvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
114 views14 pages

PHP Mysql KVR

This document discusses connecting to a MySQL database from PHP. It covers connecting to the database server, selecting a database, performing SQL queries, handling errors, building and running queries, and inserting, updating and deleting data. The document provides code examples for connecting to MySQL, selecting a database, running queries, handling errors, and retrieving and printing result rows. It also discusses combining PHP and HTML forms to submit data to a PHP script for database access.

Uploaded by

satishkvr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Database Access with

PHP and MySQL

By
Rama Satish K V
Asst. Professor, RNSIT
www.google.com/+ramasatishkv
PHP for Database Access
Connect to the MySQL server
$connection = mysql_connect("localhost",
$username, $password);
Access the database
mysql_select_db("winestore", $connection);
Perform SQL operations
Disconnect from the server
mysql_close($connection);
Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru
Error Handling
All mysql_ functions return NULL (or
false) if they fail.
Several functions are helpful in graceful
failure
die(string) - halts and displays the string
mysql_errno() - returns number of error
mysql_error() - returns text of error

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Error Handling examples
if (!($connection = mysql_connect("localhost",$name,$passwd)))
die("Could not connect");

function showerror()
{
die("Error " . mysql_errno() . " : " . mysql_error());
}

if (!(mysql_select_db("winestor", $connection)))
showerror();

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Building a Query
Directly
$query = 'select * from wines';

Using input information


$winery = $_POST['winery'];
$query = select * from wines where winery=$winery;

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Running a Query
mysql_query returns a result handle
$result = mysql_query($query, $connection)
mysql_num_rows indicates the number of
rows returned
$num_rows = mysql_num_rows($result)
mysql_fetch_array creates array/hash of
result
For ($n=0; $n<$num_rows;$n++)
$row = mysql_fetch_array($result)
Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru
Result of fetch_array
Contains both numeric and index tags
Values are duplicated
Example:
Query: select surname, city from customers;
Row: ( 0=>'Walker', surname=>'Walker',
1=>'Kent', 'city'=>'Kent' );

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Printing the Complete Row
By number
for ($i=0; $i<mysql_num_fields($result); $i++)
echo $row[$i] . " ";
By field
echo $row['surname'] . ' ' . $row['city'];

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


PHP / Form in one Document
Combine the original form with the PHP
document that processes data

if empty($regionName)) { //parameter provided?


//produce the <form>
}
else {
//run the query using data from $_GET or $_POST
}
Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru
Inserting Into a Database
Collect data from a form
Validate data (JavaScript, PHP or both)
Create a query
$query = "insert into customer set cust_id =
NULL, " . "surname =\"" . $surname ."\""
Run the query
mysql_query($query, $db);

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Updating a Database
Query to find item to update
Present old information
Collect new information
Validate
Construct and run the update query

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Demo

Sailer - Ship Case Study

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


Demo

Hotel Reservation Case Study

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru


www.google.com/+ramasatishkv

Prof. Rama Satish K V, Asst. Professor, RNSIT, Bengaluru

You might also like