PHP MySQL v1
PHP MySQL v1
Chih-Hung Lai
laich@gms.ndhu.edu.tw
2017.06.01
Table of Contents
PHP connect to mysql
Form handling
2
PHP CONNECT TO MYSQL
3
How does PHP work with mySQL
mysqli (object-oriented): only for mySQL
mysqli (procedural): only for mySQL
We use this way in this slide.
PDO: work in 12 different database systems
4
Example of mysqli (object-oriented)
5
Example of mysqli (procedural):
6
Example of PDO
7
Open database connection
Syntax
mysql_connect(server,user,passwd,database_name);
// returns 0 if success, otherwise, return error number
Last parameter can be omitted, and use mysqli_select_db( ) later
8
Error message for programmer
@: hide the connection error message
Hint: don’t show error message from the system
9
Close database connection
Close connection
Syntax
mysqli_close( connection);
Example
mysqli_close($conn);
10
ACCESS MYSQL FROM PHP
11
Performs a query against the database
mysqli_query(connection,query,resultmode);
Resultmode: optional, Either:
MYSQLI_USE_RESULT (Use this if we have to retrieve
large amount of data)
MYSQLI_STORE_RESULT (This is default)
12
Create a database & table
13
Insert data
14
Insert multiple records
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', 'mary@example.com');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', 'julie@example.com')";
if (mysqli_multi_query($conn, $sql)) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
15
Getting data from mySQL database
Returns an array of strings that corresponds to
the fetched row. NULL if there are no more rows
in result-set. Three similar functions:
mysqli_fetch_array( ): Fetches a result row as an
associative, a numeric array, or both
mysqli_fetch_assoc( ): Fetches a result row as an
associative array
mysqli_fetch_row( ): Fetches one row from a result-
set and returns it as an enumerated array
16
Get data
mysqli_fetch_array( ) can retrieve one record per time
Include the connection file (SQLconnect_inc.php)
17
Using while loop to display all records
18
Example for displaying data
Ch09-04.php
19
Example for displaying data (output)
20
Excercise
Last example use “$sql = "SELECT
`book_id`,`book_name`,`price` FROM `books`";”
to retrieve all records from the database
although the screen only displays some records
among them (perpage = 7). It decreases the
server’s performance. In fact, we only need to
get some (e.g., at most 7) records one time from
the database. Please use “limit statement” to
modify that program to solve this problem.
21
Insert data
22
Insert data (interface)
23
Exercise 2
Lastexample can insert books which have
existed. Please modify it so that it can notify
users if they want to insert existed books.
24
Ch09-07_lch.php
Delete data
25
Delete data (2)
Ch09-07-01_lch.php
26
Edit the data Ch09-08_lch.php
27
Edit the data (2) Ch09-08-01_lch.php
28
Edit the data (3)
Ch09-08-02_lch.php
29
Practice: message board
Table structure
30
Message board (home page)
31
32
Message board (second page)
33
Message board (second page)
34
Message board (third page)
35
Message board (third page)
36
FORM HANDLING
Whether is the action other file
44