I’m handcoding a personal blog in my spare time, but I’ve run across a problem. I’m trying to get the div to display the title selected from the database, but instead it just removes the div altogether (as well as the divs lower down in the code). Here’s the segment of code:
<!DOCTYPE html>
<?php
include("dbconfig.php");
?>
<html lang="EN-US">
<head>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<title>Eyes on the Ball</title>
<meta name="description" content="An online sportsblog covering a wide variety of sports.">
<meta name="keywords" content="Blog, Sportsblog, Sports, Football, NFL, Cricket, Boxing, Tennis, Other Sports, Soccer">
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="Styles.css">
<style>
#mainarticle {
background-color:orange;
width: 50%;
height: 500px;
margin: 25px;
border-style: solid;
position: absolute;
border-width: 3px;
top: 112px;
}
<?php
$statement = $mysqli->query('SELECT ID, Title FROM posts ORDER BY ID DESC');
while($row = $statement->fetch()){
echo '<div id="mainarticle">';
echo '<h1><a href="viewpost.php?id='.$row['ID'].'">'.$row['Title'].'</a> </h1>';
echo '</div>';
}
?>
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
Try
<?php
$statement = $mysqli->query('SELECT ID, Title FROM posts ORDER BY ID DESC');
var_dump($statement); // DEBUGGING ONLY
exit; // DEBUGGING ONLY
while($row = $statement->fetch()){
If a row means an entry, then yes there are four currently (which would mean there are five columns - ID, Title, Body, Category and Author.) These rows are made up of one proper article with a Title, Body, Category and Author all in the right place, and then three other entries with random letters in each one, but still having Title, Body and Category filled out.
The posts table definitely has “ID” and “Title” both spelt correctly, and both in the correct case.
Thank you for the heads up about reserved names - I’ll keep them as they are for this project just so I don’t confuse myself, but I’ll make sure to not use any reserved words in the future. I’ve enclosed them in the query for now, but I’m still getting the same error message/vardump result.
Has something changed in HTML5 that allows you to have multiple DIVs all with the same ID? I know it won’t make any difference to the problem you’re having, but I thought DIV IDs had to be unique, to use the same styling for multiple ones would use a class instead. Also (again, this may be a thing in HTML5 I haven’t got familiar with yet) are the quotes correct in these links in the nav section?
I’m thinking of how there are two after the link placeholders.
If you’re still getting the same error, have you checked whether your code to connect to the database (presumably in “dbconfig.php”) is working properly?
I suspect the issue with it reporting an error on line 157 which appears to be blank is that PHP refers to the first line as zero and your editor refers to it as one, or the other way around.
Thank you guys, I’ve fixed the problem with there being too many quotes. You mentioned that IDs had to be unique, but I can’t seem to spot where I’ve used one twice?
The code to connect to the database is definitely working, I’ve included it in other pages and it runs just fine.
My editor does indeed refer to the first line as one, which would then suggest that the error is actually on line 156, which is to do with the fetch call. Have I not used it right? It’s my first time ever trying it out, and I can’t see where I’ve gone wrong with it.
As someone mentioned above, the error in the fetch() suggests that the query hasn’t worked for some reason. So it isn’t necessarily that you’re using it incorrectly, but you should really check that the query has worked before using the result. All that would do in this case is allow you to handle the problem, though.
Does the query work in phpmyadmin or whatever equivalent you use?
I don’t use mysqli myself, but a quick read of the doc suggests that fetch() is to be used to get data from a prepared statement. If you change the code to use fetch_assoc() instead, does it work any better?