Professional Documents
Culture Documents
Seiya Tanaka
Subject:
Internet and Web Development
Contents
Database
File uploading
MySQL
MySQL is a database system used on the
web
Access to phpMyAdmin
(http://localhost/phpmyadmin/index.php)
[default setting]
Username: root
Password: root
Create a MySQL
Database
Enter a name for the database, then click
on the Create button.
table fields
DB(MySQL)
PHP PDO
Server
Connect to MySQL
<?php
$servername = "localhost";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=lesson1",
$username, $password);
echo "Connected successfully";
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// close the connection
$conn = null;
?>
Explaining the code
Try and catch : This statement for Exception
handling. A connecting to DB code should be try {
try {
$conn = new PDO("mysql:host=$servername;dbname=lesson1",
$username, $password);
echo "Connected successfully<br>";
// make a sql
$sql = "INSERT INTO login (user, pass)
VALUES ('user01', 'abcde')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// close the connection
$conn = null;
?>
Explaining the code
try {
$conn = new PDO("mysql:host=$servername;dbname=lesson1",
$username, $password);
echo "Connected successfully<br>";
// make a sql
$sql = "SELECT * FROM login";
// use query() executes an SQL statement
foreach($conn->query($sql) as $row) {
echo $row['user'] . " ";
echo $row['pass'] . "<br>";
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// close the connection
$conn = null;
?>
Explaining the code
The foreach loop works only on arrays, and is used to loop through
each key/value pair in an array
foreach($conn->query($sql) as $row) {
echo $row['user'] . " ";
echo $row['pass'] . "<br>";
}
Practice
login.html result.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=lesson1",
$username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE,
PDO::ERRMODE_EXCEPTION);
echo "Connected successfully<br>";
// make a sql
$sql = "SELECT * FROM login";
$stmt = $conn->query($sql);
// use query() executes an SQL statement
if($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['user'] . " is login.";
}else{
echo "no user or no password";
}
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
// close the connection
$conn = null;
?>
Explaining the code
$stmt->fetch(PDO::FETCH_ASSOC)
File uploading
file_uploads = On
Create The HTML Form
Next, create an HTML form that allow users
to choose the image file they want to
upload:
<!DOCTYPE html>
<html>
<body>
<?php
$target_file = "uploads/".$_FILES["fileToUpload"]["name"];
if(isset($_POST["submit"])) {
if (!move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "faild to copy";
exit;
}
echo "success to copy";
}
?>
Explaining the code
$_FILES["fileToUpload"]["name"] returns file
name
$_FILES["fileToUpload"]["tmp_name"] returns
temp file of uploaded file
Note: You will need to create a new directory called "uploads" in the
directory where "upload.php" file resides and change the directory auth to
"777". The uploaded files will be saved there.
Summary
Database
File uploading