Reputation: 548
I am currently redesigning my website to use PHP and databases (opposed to Bootstrap, CSS, and HTML) and I am currently having issues grabbing my links from my database and putting them in my nav menu. I prefer to use a database to prevent the need of re-entering all the links if I eventually decide to change them. The issue is I can't seem to get any HTML output here, and I'm not sure what I'm doing wrong. I am fairly new to PHP so there may be something trivial that I just haven't grasped the concept of but currently this code only displays the bullet point of the <ul>
on the page. My database and code are below. The database is confirmed able to connect.
test.php:
require_once('config.php');
$conn = new mysqli($servername, $username, $password, $database);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT link_title, link_url FROM links ORDER BY link_id";
if ($result = $conn->query($query)) {
echo '<ul class="nav navbar-nav navbar-right">';
foreach($result as $link) {
echo '<li>';
echo "<a href='{$link->link_url}'>{$link->link_title}</a>\n";
echo '</li>';
}
echo '</ul>';
/* free result set */
$result->close();
}
/* close connection */
$conn->close();
Upvotes: 0
Views: 1025
Reputation: 1023
if ($result = $conn->query($query)) {
echo '<ul class="nav navbar-nav navbar-right">';
while($link = $result->fetch_object()) {
echo '<li>';
echo "<a href='{$link->link_url}'>{$link->link_title}</a>\n";
echo '</li>';
}
echo '</ul>';
/* free result set */
$result->free(); // there is no method "close" for result -> http://fi2.php.net/manual/en/class.mysqli-result.php
}
Upvotes: 1