I am having issues trying to figure out why my $query2
is failing. I have looked this code over and over, but can't figure it out.
This is the line of code that is failing..
$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE category-id='".$cid."' ORDER BY topic_reply_date DESC")
or die ("Query2 failed: %s
".($query2->error));
Does anyone see something that I am missing that would make this fail?
$user = new User();
if(!$user->isLoggedIn()) {
Redirect::to('index.php');
}
$logged = $user->isLoggedIn();
$con = mysqli_connect("localhost", "root", "", "db");
$cid = $_GET['cid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
if(isset( $_SESSION['user'])) {
$logged = " | <a href='forum_create_topic.php?cid=".$cid."'>Create a new topic</a>";
}
$query = mysqli_query($con,"SELECT * FROM forum_categories WHERE id='".$cid."' LIMIT 1");
$numrows = mysqli_num_rows($query);
if($numrows == 1){
$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE category-id='".$cid."' ORDER BY topic_reply_date DESC")
or die ("Query2 failed: %s
".($query2->error));
$numrows2 = mysqli_num_rows($query2);
category-id
needs to be escaped.
Use backticks `
to escape the column-name like this: `category-id`
. I.e.:
WHERE `category-id` ='".$cid."'
MySQL is translating that as "category
MINUS id
" in thinking you want to do math.
Your sql should look like this:
$query2 = mysqli_query($con,"SELECT * FROM forum_topics WHERE `category-id`='".$cid."' ORDER BY topic_reply_date DESC")
or die ("Query2 failed: %s
".($query2->error));