Lab07 (8) - PHP05-PDO-Connect V1.01 - Updated
Lab07 (8) - PHP05-PDO-Connect V1.01 - Updated
Php-Pdo
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.
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);
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>