You are on page 1of 44

PHP &mySQL

Chih-Hung Lai
laich@gms.ndhu.edu.tw
2017.06.01
Table of Contents
 PHP connect to mysql

 Access mySQL from PHP

 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

Hint: Earlier versions of PHP used the mysql


extension, not mysqli (before 2012)

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)

$row will be an one-dimension array

17
Using while loop to display all records

18
Example for displaying data
 Ch09-04.php

//2 dimensional array

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

 in a file with a form (e.g. index.html or index.php)


 <form method="post" action=”welcome.php”>
 Can we call the file itself?
Example (version 1)
 Hello.html
 <html>
<body>
<form action=“welcome.php” method=“post”>
Name: <input type=“text” name=“name”><br>
E-mail: <input type=“text” name=“email”><br>
<input type=“submit”>
</form>
</body>
</html>
 welcome.php
 <html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>
Example (version 2)
 Hello.html
 <html><body>
<form action=“welcome2.php” method=“post”>
Name: <input type=“text” name=“name”><br>
E-mail: <input type=“text” name=“email”><br>
<input type=“submit”>
</form> </body></html>
 welcome2.php
 <html><body>
<form method="post" action="welcome2.php">
Name:<input type="text" name="name"><br><br>
E-mail:<input type="text" name="mails"><br>
<input type="submit" value = "submit">
</form>
<?php
echo $_POST["name"];
echo "<br>Your email address is: “ . $_POST["mails"];
?>
Example (version 3)
 welcome.php
<html>
<body>
<form method="post" action="welcome3.php">
Name:<input type="text" name="name"><br><br>
E-mail:<input type="text" name="mails"><br>
<input type="submit" value = "submit">
</form>
<?php
if (isset($_POST['name']) && isset($_POST['name'])){
// isset( ) can check whether the variable exists
echo $_POST["name"];
echo "<br>Your email address is: ";
echo $_POST["mails"];
}
?>
</body>
</html>
Predefined variables
 phpinfo( )
 $_SERVER['PHP_SELF’]
<?php
phpinfo();
echo $_SERVER['PHP_SELF'];
?>
Example (version 4)
 welcome.php
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Name:<input type="text" name="name"><br><br>
E-mail:<input type="text" name="mails"><br>
<input type="submit" value = "submit">
</form>
<?php
if (isset($_POST['name']) && isset($_POST['name'])){
echo “Welcome “ . $_POST["name"];
echo "<br>Your email address is: ";
echo $_POST["mails"];
}
?>
</body>
</html>
MySQLi - Useful Functions
 https://www.w3schools.com/php/php_ref_mysqli.
asp

44

You might also like