This sample is displayed as source code. Executing it requires a PHP-enabled server.
<?php
/*
PART 1: REQUIRED. Place ONCE at top of file (before html)
Creates a connection to the database (with login info)
*/
$host = "webdev.iyaserver.com"; // database server address
$userid = "dent_student"; // database user account name
$userpw = "code4Studentuse"; // database user password
$db = "dent_movies"; // database name
$mysql = new mysqli(
$host,
$userid,
$userpw,
$db);
if($mysql->connect_errno) {
echo "db connection error : " . $mysql->connect_error;
exit();
}
?> <!-- end of php block -->
<?php
/*
PART 2: REQUIRED. Can be repeated if multiple queries
write a database query and get back resuts from the database
*/
$sql = "SELECT * from view1 WHERE 1=1 AND label = 'Dreamworks' ";
// potentially add to $sql with additional WHERE filters like
// $sql .= " AND column = 'data' OR column like '%term%'";
// potentially add a sort order clause
// $sql .= " ORDER BY columnname";
$results = $mysql->query($sql);
if(!$results) {
echo "SQL error: ". $mysql->error . " running query <hr>" . $sql . "<hr>";
exit();
}
// close php block
?>
<!--
PART 3: OPTIONAL. Output number of database results/rows
PLACE in your page the code below that outputs number of records in query.
-->
<?= $results->num_rows ?>
<!--
Part 4: Note use EITHER
* Part4a for data/select * looped (multi-row) queries OR
* Part4b for single-row stat/aggregate queries
-->
<!-- PART 4a: Loop of rows of database results
PLACE the following code block in your page where you want the results to appear.
It creates an output LOOP. It requires that you have already created a query
and stored it in php variable $results.
NOTE you need to change "columnname" to a db output column/field name
-->
<?php
while( $currentrow = $results->fetch_assoc() ) { // OPEN/active php loop
?>
<!-- normal html and text, with php output blocks for column values -->
<?= $currentrow["columnname"] ?> <!-- NOTE: need to CHANGE columnname -->
<?php
} // end php loop
?>
<!-- Part 4b: Output single stat values from a single-row aggregate query -->
<?php
$currentrow = $results->fetch_assoc();
?>
Stat1: <?= $currentrow["columnname"] ?>
<!-- note: columnname above must be an alias (AS) to aggr sql parameter -->