简体   繁体   中英

Query to get 2 columns in same table

I'm completely stuck on this bit of code and hoping someone can help. I have a database with one table called "pages" and it has 5 columns: id, file_name link_name, category, main_cat. Not sure if it matters but main_cat has type of tinytext and file_name is varchar.

id | file_name   | link_name   | category | main_cat 
1  | books       | Books       | books    | 1
2  | fiction     | Fiction     | books    | 2
3  | non-fiction | Non-Fiction | books    | 2
4  | music       | Music       | music    | 1
5  | alternative | Alternative | music    | 2
6  | country     | Country     | music    | 2

In my navigation menu(code below), I would like to have all of the main_cat(1) listed and then the sub-pages, man_cat(2) to show underneath. Here is the code I have, right now, I've tried many different ways including an inner join and left join but not matter what, I keep getting an error that says "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in..."

<ul>
 <?php
   $query = "SELECT * FROM pages WHERE main_cat = 1 ORDER BY link_name ASC ";
   $main_pages = mysqli_query($conn, $query);
   confirm_query($main_pages);
 ?>
 <?php
   while($mainpage = mysqli_fetch_assoc($main_pages)) {
 ?>
  <li><a href="/edit.php?<?php echo $mainpage["id"]; ?>"><?php echo $mainpage["link_name"]; ?></a>  

  <?php
$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = {$mainpage["file_name"]}";
$sub_pages = mysqli_query($conn, $query);
confirm_query($sub_pages);
  ?>
  <ul>
  <?php
while($subpage = mysqli_fetch_assoc($sub_pages)) {
  ?>
 <li><a href="/edit.php?<?php echo $subpage["id"]; ?>"><?php echo $subpage["link_name"]; ?></a></li>    
 <?php
}
 ?>
  <?php mysqli_free_result($sub_pages); ?>
   </ul>
 </li>
 <?php
}
 ?>
 <?php mysqli_free_result($main_pages); ?>
 </ul>      

This is going to give you heartburn:

$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = {$mainpage["file_name"]}";

The problem is that the " characters around file_name are being interpreted as ending the string, and PHP is behaving unpredictably. In other words, PHP sees this:

$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = {$mainpage["file_name"]}";
      // ^ start string                                                    ^ end     ^s ^end again

Try one of these options instead:

$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = '{$mainpage['file_name']}'";
$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = '".$mainpage["file_name"]."'";

Also, note the quotation marks around the variable -- unless file_name is actually a number, you need to enclose the data with quotation marks.

Finally, this probably goes without saying, but you really need to use prepared statements here.

Check if you have connected the right database and your queries are syntactically correct

$main_pages = mysqli_query($conn, $query); will return boolean if your $conn is not set up correctly or query syntax is wrong.

Hence when you fire mysqli_fetch_assoc($main_pages) it throws "Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in..." as, $main_pages is boolean value.

I think it is something to do with your second query, you may have it formatted wrong... I'm no expert but maybe someone else can edit if I have it wrong.

From this:

$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = {$mainpage["file_name"]}";

To this:

$mainfilename = $mainpage['file_name'];

$query = "SELECT * FROM pages WHERE main_cat = 2 AND category = $mainfilename";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM