PHP and Mysql - Unit-V
PHP and Mysql - Unit-V
MYSQL:
MySQL Features:
There are too many differences between these PHP database extensions.
These differences are based on some factors like performance, library
functions, features, benefits, and others.
MySQL MySQLi
Making a Connection:
$mysqli = mysqli_connect(“hostname”,”username”,”password”,”database”);
The value of $mysqli is the result of the function and is used in later
functions for communicating with MySQL.
With sample values inserted, the connection code looks like this:
$mysqli = mysqli_connect(“localhost”,”ashaz”,”password”,”TestDB”);
Example Program:
<?php
If(mysqli_connect_errno())
exit();
else
?>
In the above example, it is creates a new connection in line 2 and then tests to
see whether an error occurred. If an error occurred, line 5 prints an error
message and uses the mysqli_connect_error() function to print the message. If
no error occurs, line 8 prints a message that includes host information
resulting from calling the mysqli_get_host_info() function.
Executing Queries:
Half the battle in executing MySQL queries using PHP is knowing how to
write the SQL.
The mysqli_query() function in PHP is used to send your SQL query to
MySQL.
Example Program:
<?php
If(mysqli_connect_errno())
exit();
else
testField VARCHAR(75))”;
$res=mysqli_query($mysqli,$sql);
if($res == TRUE)
{
echo “Table testTable successfully created”;
else
mysqli_close($mysqli);
?>
In lines 10 – 12, the text that makes up the SQL statement is assigned to
the variable $sql.
The mysqli_query function returns a value of true or false, and this value
is checked in the if..else statement beginning in line 15.
If the value of $res is true, a success message is printed to the screen.
However, if the value of $res is not true and the table was not created,
an error message appears, generated by the mysqli_error() function.
The mysqli_close() function is used to close the connection.
Inserting, updating, deleting and retrieving data all revolve around the
use of the mysqli_query() function to execute the basic SQL queries.
The basic query commands like INSERT, UPDATE, and DELETE queries,
no additional scripting is required after the query has been executed
because you’re not displaying any results (unless you want to).
When using SELECT queries, you have a few options for displaying the
data retrieved by your query.
The easiest method for inserting data at this stage in the game is to
simply hard – code the INSERT statement.
<?php
If(mysqli_connect_errno())
exit();
else
$res = mysqli_query($mysqli,$sql);
If($res == TRUE)
else
mysqli_close($mysqli);
?>
Here SQL query stored in the $sql variable on line 10 and text
modifications on lines 14 and 18.
The connection code and the structure for issuing a query remain
the same.
In fact, most procedural code for accessing MySQL fails into this same
type of code template.
Call this script mysqlinsert.php and place it on your webserver.
Running this script results in the addition of a row to the testTable table.
You can either make a long list of hard – coded SQL statements and use
mysqli_query() multiple times to execute these statements.
Because you have a few rows in your testTable table, you can write
a PHP script to retrieve that data.
Starting with the basics, we write a script that issues a SELECT query but
doesn’t over when you with result data.
To get the number of rows then use mysqli_num_rows() function.
Example Program:
<?php
If(mysqli_connect_errno())
exit();
else
$res = mysqli_query($mysqli,$sql);
If($res)
{
$numver_of_rows=mysqli_num_rows($res);
else
mysqli_free_result($res);
mysqli_close($mysqli);
?>
Update data:
Syntax:
UPDATE table_name
WHERE someColumn=someValue ;
Example:
<?php
$con=mysqli_connect("example.com","alex","qwerty","db_name");
// Check connection
if (mysqli_connect_errno($con))
?>
Delete data:
The DELETE FROM statement is used to delete data from a database table.
Syntax:
Example:
<?php
$con=mysqli_connect("example.com","alex","qwerty","db_name");
// Check connection
if (mysqli_connect_errno($con))
{
echo "MySQL database connection failed: " . mysqli_connect_error();
?>
When you think of an address book, the obvious fields come to mind: name,
address, telephone number, email address. However, if you look at (or
remember) a paper-based address book, you might note there are several
entries for one person. Maybe that person has three telephone numbers, or
two email addresses, and so forth—whatever didn’t fit in the original template.
In an online address book, a set of related tables helps alleviate the
redundancy and repetition of information and allows you to display all
information in a unified view.
Names
fax_number, type
date_modified, note
date_added DATETIME,
date_modified DATETIME,
);
date_added DATETIME,
date_modified DATETIME,
);
date_added DATETIME,
date_modified DATETIME,
);
date_added DATETIME,
date_modified DATETIME,
);
date_added DATETIME,
date_modified DATETIME,
);
date_added DATETIME,
date_modified DATETIME,
note TEXT
);
Now that your tables are created, you can work through the forms and scripts
for managing and viewing your records.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p><strong>Management</strong></p>
<ul>
<li><a href=”addentry.php”>Add an Entry</a></li>
</ul>
<p><strong>Viewing</strong></p>
<ul>
</ul>
</body>
</html>
1: <?php
2: if ($_POST[op] != "add") {
6: <P><strong>First/Last Names:</strong><br>
9:
10: <P><strong>Address:</strong><br>
12:
13: <P><strong>City/State/Zip:</strong><br>
17:
22:
28:
34:
40:
44:
46: </FORM>";
47:
52: exit;
53: }
54:
57: or die(mysql_error());
64:
67:
69: ($_POST[zipcode])) {
75: }
76:
77: if ($_POST[tel_number]) {
83:
84: if ($_POST[fax_number]) {
89: }
90:
91: if ($_POST[email]) {
96: }
97:
98: if ($_POST[note]) {
103: }
104:
105: $display_block = "<h1>Entry Added</h1>
108: }
109: ?>
110: <HTML>
111: <HEAD>
113: </HEAD>
114: <BODY>
116: </BODY>
117: </HTML>
9. Discuss about Viewing Records.
1: <?php
2: //connect to database
4: or die(mysql_error());
5: mysql_select_db("testDB",$conn) or die(mysql_error());
6:
7: if ($_POST[op] != "view") {
10:
15:
19:
20: } else {
27:
31:
33: $display_name</option>";
34: }
36: </select>
40: </FORM>";
41: }
42:
44:
45: //check for required fields
48: exit;
49: }
50:
56: 0,'display_name'));
62:
64:
66: <ul>";
67:
68: while ($add_info = mysql_fetch_array($get_addresses_res)) {
74:
76: ($address_type)";
77: }
78:
80: }
81:
86:
88:
90: <ul>";
91:
95:
97: }
98:
100: }
101:
106:
108:
110: <ul>";
111:
115:
117: }
118:
120: }
121:
126:
128:
130: <ul>";
131:
135:
138:
140: }
141:
146:
147: if (mysql_num_rows($get_notes_res) == 1) {
149:
151: }
152:
155: }
156: ?>
157: <HTML>
158: <HEAD>
161: <BODY>
163: </BODY>
164: </HTML>
48: exit;
49: }
50:
53: mysql_query($del_master);
54:
56: mysql_query($del_address);
57:
59: mysql_query($del_tel);
60:
62: mysql_query($del_fax);
63:
65: mysql_query($del_email);
66:
68: mysql_query($del_master);
69:
74: ?>
75: <HTML>
76: <HEAD>
78: </HEAD>
79: <BODY>
81: </BODY>
82: </HTML>
1: <?php
5: <h1>Add an Entry</h1>
7:
8: if ($_GET[master_id] != "") {
9: //connect to database
11: or die(mysql_error());
13:
18:
19: if (mysql_num_rows($get_names_res) == 1) {
21: }
22: }
23:
26: <strong>$display_name</strong>:</p>";
27: } else {
32: }
35:
36: <P><strong>City/State/Zip:</strong><br>
40:
45:
51:
57:
63:
68:
70: </FORM>";
71:
77: exit;
78: }
79:
82: or die(mysql_error());
84:
92: } else {
94: }
95:
96: if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) ||
97: ($_POST[zipcode])) {
103: }
104:
105: if ($_POST[tel_number]) {
110: }
111:
112: if ($_POST[fax_number]) {
117: }
118:
119: if ($_POST[email]) {
124: }
125:
126: if ($_POST[note]) {
131: }
132:
136: }
137: ?>
138: <HTML>
139: <HEAD>
141: </HEAD>
142: <BODY>
</BODY>
145: </HTML>