0% found this document useful (0 votes)
11 views6 pages

Lab07 (8) - PHP05-PDO-Connect V1.01 - Updated

Uploaded by

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

Lab07 (8) - PHP05-PDO-Connect V1.01 - Updated

Uploaded by

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

LAB 07 (8)

Php-Pdo

PDO (Php Data Objects)


is a lean, consistent way to access databases. This means developers can write portable code
much easier.

1. From phpMyAdmin or command line, create the database and user. Replace 'chew'
with your name. You may replace 'zap' with your own password as well.

CREATE DATABASE misc;


GRANT ALL ON misc.* TO 'chew'@'localhost' IDENTIFIED BY 'zap';
GRANT ALL ON misc.* TO 'chew'@'127.0.0.1' IDENTIFIED BY 'zap';

2. Create a table call users in your database.

CREATE TABLE users (


user_id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(128),
email VARCHAR(128),
password VARCHAR(128),
PRIMARY KEY(user_id),
INDEX(email)
) ENGINE=InnoDB CHARSET=utf8;

3. Insert some records into your table.

INSERT INTO users (name,email,password) VALUES


('chew','[email protected]','123');
INSERT INTO users (name,email,password) VALUES
('siti','[email protected]','456');

1
4. Use the following php code, make a connection to the database server. Retrieve some
data from the database, and print it out using print_r.
labXX-p1.php
<?php
echo "<pre>\n";
$pdo=new PDO('mysql:host=localhost;port=3306;dbname=misc',
'chew', 'zap');
$stmt = $pdo->query("SELECT * FROM users");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($rows);
echo "</pre>\n";
?>

5. Instead of using print_r, use "echo" and format the sql data retrieved into a table
accordingly. Screenshot the output from your browser
labXX-p1.php
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'chew', 'zap');
$stmt = $pdo->query("SELECT user_id, name, email, password
FROM users");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<table border="1">'."\n";
foreach ( $rows as $row ) {
echo "<tr><td>";
echo($row['user_id']);
echo("</td><td>");
echo($row['name']);
echo("</td><td>");
echo($row['email']);
echo("</td><td>");
echo($row['password']);
echo("</td></tr>\n");
}
echo "</table>\n";
?>

2
Separating connection from main implementation (Pattern)

1. Use the previous code (the sql data retrieved and formatted accordingly to HTML),
separate the connection file and php implementation. Screenshot the output of your
code and browser.
- Use require_once at the beginning the previous code
- Remove the $pdo from the previous code
pdo.php
<?php
$pdo = new PDO('mysql:host=localhost;port=3306;dbname=misc',
'chew', 'zap');
// See the "errors" folder for details...
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Visit the website below to check more errmode option


//errmode_warning – it will show the error, but continue run
the code
//errmode_exception – show the warning, and stop the code
//http://php.net/manual/en/pdo.error-handling.php

labXX-p1.php
<?php
require_once "pdo.php";
$stmt = $pdo->query("SELECT user_id, name, email, password
FROM users");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo '<table border="1">'."\n";
foreach ( $rows as $row ) {
echo "<tr><td>";
echo($row['user_id']);
echo("</td><td>");
echo($row['name']);
echo("</td><td>");
echo($row['email']);
echo("</td><td>");
echo($row['password']);
echo("</td></tr>\n");
}
echo "</table>\n";
?>

3
Inserting row into Database using Php

1. Continue from the previous code, use the following code, try to add at least two new
users into the database.
- Make sure pdo.php has been configured accordingly
- Isset is just make sure the variable is set and is not null, however, if you keep the
box empty, the variable is still set, name="", it's just does not contain any
information inside, you need to do extra server input validation (refer to lab07) to
prevent this from happening
- Isset → make sure that the current page know which button or which information
is being submitted

labXX-p1.php
<?php
require_once "pdo.php";
if ( isset($_POST['name']) && isset($_POST['email'])
&& isset($_POST['password'])) {
$sql = "INSERT INTO users (name, email, password)
VALUES (:name, :email, :password)";
echo("<pre>\n".$sql."\n</pre>\n");
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':name' => $_POST['name'],
':email' => $_POST['email'],
':password' => $_POST['password']));
}
?>
<html>
<head></head>
<body>
<p>Add A New User</p>
<form method="post">
<p>Name:<input type="text" name="name" size="40"></p>
<p>Email:<input type="text" name="email"></p>
<p>Password:<input type="password" name="password"></p>

4
<p><input type="submit" value="Add New"/></p>
</form>
</body>
</html>

5
Delete row from database using Php

1. Try out the following code and delete one user that has been created previously.
Screenshot the list before and after deleting.
labXX-p1.php
<?php
require_once "pdo.php";

if ( isset($_POST['user_id']) ) {
$sql="DELETE FROM users WHERE user_id = :zip";
echo "<pre>\n$sql\n</pre>\n";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(':zip'=>$_POST['user_id']));
}
?>
<p>Delete A User</p>
<form method="post"><p>ID to Delete:
<input type="text" name="user_id"></p>
<p><input type="submit" value="Delete"/></p>
</form>

You might also like