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?
答案 0 :(得分: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: