This is an old revision of the document!
In this activity you will use PHP to connect to the database and run a query.
<?php
// begin HTML code
include('header.php');
include('menu.php');
?>
<?php
// finish HTML code
include('footer.php');
?>
audio has built-in functions like .play(), .paused() and built-in information like .paused. That's because audio is an object. [] , we access information and functions using ->.$table->legs = 4;
$table->move($location);
// Set up database connection
define ('DBCONNECT', "../../pdo.php"); // database connection
include_once DBCONNECT;
$db = 'webdev';
$dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
try
{ // connect
$pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
} catch (\PDOException $e) {
throw new \PDOException ($e->getMessage(), (int)$e->getCode());
}
define ('DBCONNECT', "../../pdo.php");) defines a static (unchangeable variable) called DBCONNECT that holds the relative address of a file outside the web server. This file holds our host, username and password for the database as well as the options we'll use to connect. We don't want these available on the server because if PHP fails for any reason, our full code will be exposed to users and a malicious person could use this information to access our database and ruin our site or use our server for illegal activities. By storing the data outside of the web server, the user can never see this detail.include DBCONNECT;$db = 'webdev';) because each application / site could use a different database so we need to define it inside the application / site.$dsn = "mysql:host=$db_host;dbname=$db;charset='utf8mb4'";$pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);, where $pdo is an object that includes both the database connection and the functions needed to access the data.try / catch block to handle any errors that may occur if the connection fails. In such a case, our code will throw an exception that includes a message and the error code.include('menu.php');. $query = "SELECT heroes.character_name, hero_villain, first_appeared, power, movie
FROM heroes
LEFT JOIN appearances ON heroes.character_name=appearances.character_name";
$args = array();
$rslt = $pdo->prepare($query);
$rslt is now an object that holds the result of our query.$rslt->execute($args);
$rslt->execute($args); while ($row = $rslt->fetch()) {
}
$rslt and stores it in the variable $row.{}. error_log("MURRAY: query response row - " . print_r($row,1));
print_r.$row is the content to be displayed. 1 tells print_r to convert the first parameter to a string, which can then be displayed using echo or error_log. <main>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hero / Villain</th>
<th>First Appearance</th>
<th>Power</th>
<th>Movie</th>
</tr>
</thead>
<tbody>
</body>
</table>
</main>
<main>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hero / Villain</th>
<th>First Appearance</th>
<th>Power</th>
<th>Movie</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $rslt->fetch()) {
error_log("MURRAY: query response row - " . print_r($row,1));
}
?>
</tbody>
</table>
</main>
<?php
while ($row = $rslt->fetch()) {
error_log("MURRAY: query response row - " . print_r($row,1));
echo "\t\t\t\t<tr>\n";
echo "\t\t\t\t\t<td>$row['character_name']</td>\n";
echo "\t\t\t\t\t<td>$row['hero_villain']</td>\n";
echo "\t\t\t\t\t<td>$row['first_appeared']</td>\n";
echo "\t\t\t\t\t<td>$row['power']</td>\n";
echo "\t\t\t\t\t<td>$row['movie']</td>\n";
echo "\t\t\t\t</tr>\n";
}
?>
<?php
while ($row = $rslt->fetch()) {
?>
<tr>
<td><?php echo $row['character_name'];?></td>
<td><?php echo $row['hero_villain'];?></td>
<td><?php echo $row['first_appeared'];?></td>
<td><?php echo $row['power'];?></td>
<td><?php echo $row['movie'];?></td>
</tr>
<?php
}
?>
$hero_data[$row['character_name']]['hero_villain'] = $row['hero_villain'];
$hero_data for all the heroes and inside that, we are creating an array for each character ($hero_data[$row['character_name']]).$hero_data['Black Widow']['hero_villain'] will be 'Hero' every time, so we don't mind if it's overwritten. $hero_data[$row['character_name']]['first_appeared'] = $row['first_appeared'];
$hero_data[$row['character_name']]['power'] = $row['power'];
$hero_data[$row['character_name']]['movies'] .= $row['movie'] . "<br>";
<br> added after each movie will move each to its own line in the table cell so it's easier for the user to read.$hero_data array. foreach($hero_data as $name => $details) {
}
$hero_data is a nested array (an array within an array), $details itself is an array.$name and the $details array. <tr>
<td><?php echo $name;?></td>
<td><?php echo $details['hero_villain'];?></td>
<td><?php echo $details['first_appeared'];?></td>
<td><?php echo $details['power'];?></td>
<td><?php echo $details['movies'];?></td>
</tr>
<?php
// Set up database connection
define ('DBCONNECT', "../../pdo.php"); // database connection
include DBCONNECT;
$db = 'webdev';
$dsn = "mysql:host=$db_host;dbname=$db;charset=utf8mb4";
try
{ // connect
$pdo = new MyPDO($dsn, $db_user, $db_pass, $db_options);
} catch (\PDOException $e) {
throw new \PDOException ($e->getMessage(), (int)$e->getCode());
}
// begin HTML code
include('header.php');
include('menu.php');
// extract data
$query = "SELECT heroes.character_name, hero_villain, first_appeared, power, movie
FROM heroes
LEFT JOIN appearances ON heroes.character_name=appearances.character_name";
$args = array();
$rslt = $pdo->prepare($query);
$rslt->execute($args);
?>
<main>
<table>
<thead>
<tr>
<th>Name</th>
<th>Hero / Villain</th>
<th>First Appearance</th>
<th>Power</th>
<th>Movies</th>
</tr>
</thead>
<tbody>
<?php
while ($row = $rslt->fetch()) {
$hero_data[$row['character_name']]['hero_villain'] = $row['hero_villain'];
$hero_data[$row['character_name']]['first_appeared'] = $row['first_appeared'];
$hero_data[$row['character_name']]['power'] = $row['power'];
$hero_data[$row['character_name']]['movies'] .= $row['movie'] . "<br>";
}
foreach($hero_data as $name => $details) {
?>
<tr>
<td><?php echo $name;?></td>
<td><?php echo $details['hero_villain'];?></td>
<td><?php echo $details['first_appeared'];?></td>
<td><?php echo $details['power'];?></td>
<td><?php echo $details['movies'];?></td>
</tr>
<?php
}
?>
</body>
</table>
</main>
<?php
// finish HTML code
include('footer.php');
$i = 0;
while ($i < 10) {
error_log("MURRAY: the value of i is $i");
$i++;
}
$my_array = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit');
foreach($my_array as $name => $item) {
error_log("MURRAY: $name is a $item");
}
?>