-3

I'm making a page similar to forums. I need sections and sub-sections and threads. Here is my code:

<?php
    $query = "select * from sections";
    $result = mysql_query($query);
    while ($name = mysql_fetch_array($result)) {
        echo '<div class="title">'.$name["Name"].'</div>';
        #$queryr = ;
        #$resultr = ;

        while ($resultre = mysql_fetch_array(mysql_query("SELECT * FROM sub-sections"))) {
            if ($resltre['idLinkedTo'] == $name['ID'])
            {
                echo '<div class="box"><a href="showSection.php?id='. $resultre['ID'] .'">'. $resultre['Name'] .'</a>';
                echo '####</div> </br>';
            }
        }
    }
?>

Problem:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:------- on line 35

How do I fix this error?

3dgoo
  • 15,546
  • 5
  • 45
  • 57
Kinan Jano
  • 13
  • 2
  • 4
    Firstly, it's a typo `resltre` then this too `sub-sections` --- http://php.net/manual/en/function.mysql-error.php - You don't want MySQL to do math, right? – Funk Forty Niner Sep 01 '15 at 22:10
  • You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-sections' at line 1 What does that mean ? – Kinan Jano Sep 01 '15 at 22:17

1 Answers1

4

Ok, before this gets out of whack...

Your table name, MySQL figures you want to do math here.

SELECT * FROM sub-sections
                 ^ it has a special meaning

which translates to sub minus sections.

Use ticks around the table name

SELECT * FROM `sub-sections` ...

then a typo in $resltre which should read as $resultre

You are also open to SQL injection. Use a prepared statement:

Make sure you are indeed using the same MySQL API as the query.

Different APIs do not intermix. Use the same one from connecting to querying.

Yet, mysql_ is deprecated. Best you move to mysqli_ or PDO.


Error checking:

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Also add or die(mysql_error()) to mysql_query().

References:

Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132