-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamplequery.php
More file actions
executable file
·37 lines (33 loc) · 1.42 KB
/
examplequery.php
File metadata and controls
executable file
·37 lines (33 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
//Connect to MySQL server, with the username 'cpsc471' and password 'homepage', to the database called 'cpsc471'
$mysqli = new mysqli("localhost", "cpsc471", "homepage","cpsc471");
//If there is a connection error, stop executing the script
if ($mysqli->connect_error) {
//Print out the connection error
die('Connect Error [' . $mysqli->connect_errno . '] ' . $mysqli->connect_error);
}
//Set a string variable
$query = "SELECT * FROM cars WHERE make='dodge'";
//Equivalent to '$query = $query + " example"'
//The period is the concatenation operator
//$query .= " example";
//Query the server, which will return a PHP Object that contains the results of the query
$result = $mysqli->query($query);
//Set $row to the next line. If it returns null (there is no next line), exit the loop.
while ($row = $result->fetch_assoc()) {
//For each row
//Output some HTML
echo '<div class="infoblock">';
//For each column, set $key to be equal to the column name and set $val to be the value in the cell of that column
foreach($row as $key => $val) {
//Output some HTML
echo "<p>$key: $val</p>";
}
//Remember to close your HTML tags
echo "</div>";
}
//Free the memory
$result->free();
//Close the connection
$mysqli->close();
?>