Code: Select all
function db_connect() {
// Create new database connection -host, -username, -password, -database name
@ $conn = new mysqli ('host', 'username', 'password', 'db');
if (mysqli_connect_errno()) {
echo "<p>Error: Could not connect to the database. Try again later.</p>";
exit;
}
return $conn;
}
Code: Select all
function entries($tag, $page, $postNumber) {
//Database Query
$conn = db_connect();
$query = "SELECT * FROM entries, users WHERE entries.userId = users.userId ORDER BY timestamp DESC LIMIT $from, $postNumber";
$result = $conn->query($query);
//Display Entries
if ($result->num_rows > 0) {
while ($row = $result->fetch_object()) {
echo "Posted By ".$row->name;
}
}
//If there are no entries in the database (prevent infinate loop)
else if (!($result->num_rows > 0) && ($page == 1)) {
echo "<dt>We're sorry...</dt>";
echo "<dd>There are no entries in the database yet... Please check again later.</dd>";
}
//If that page does not exist
else {
entries($tag, 1, $postNumber);
}
//Close Database
$conn->close();
}