1

I just can't figure out why i get the error message, I have tried removing the'' and the()

I have run the script in phpmyadmin and it says the problem with my syntax is at $result = ("SELECT * FROM 'test_prefixCatagory' ORDER by 'Cat'");

$result = ("SELECT * FROM 'test_prefixCatagory' ORDER by 'Cat'");


while($row = mysql_fetch_array($result))

$sCat = ($row['Cat']);
$sCatID = ($row['CatID']);
{
echo "<table>";
 echo "<tr valign='top'><td><b><a href='#".$sCat."'>".$sCat."</a></b><br>";
 // column 1 categories
 $result2 = ("SELECT * FROM `test_prefixSubCat` WHERE `CatID`=$sCatID");
 // sub-cats
 while($row2 = mysql_fetch_array($result2)) 
    {
  $sSub = ($row2['CatID']);
  $sSubID = ($row2['SubID']);
  echo "<dd><a href='#'>".$sSub."</a><br>";

 }

 echo "<br></td></tr>";
echo "</table>";
 }

Do anyone have an idea?

1
  • You should no longer listen to the one that told you to put brackets around everything like ($row2['CatID']). Commented May 17, 2012 at 14:02

3 Answers 3

1

Try this :

<?php
$result = mysql_query("SELECT * FROM `test_prefixCatagory ORDER by `Cat`");

while ($row = mysql_fetch_array($result)) {
$sCat = $row['Cat'];
$sCatID = $row['CatID'];
echo "<table>";
echo "<tr valign='top'><td><b><a href='#" . $sCat . "'>" . $sCat . "</a></b><br>";
// column 1 categories
$result2 = mysql_query("SELECT * FROM `test_prefixSubCat` WHERE `CatID`='".$sCatID. "'");
// sub-cats
while ($row2 = mysql_fetch_array($result2)) {
    $sSub = $row2['CatID'];
    $sSubID = $row2['SubID'];
    echo "<dd><a href='#'>" . $sSub . "</a><br>";

    }

        echo "<br></td></tr>";
        echo "</table>";
}
?>
Sign up to request clarification or add additional context in comments.

3 Comments

actually both results need fixing according to d4rkpr1nc3's suggestion
yes, sorry, i've stop looking down after seeing the first line
$result = mysql_query("SELECT * FROM test_prefixCatagory ORDER by Cat"); while($row = mysql_fetch_array($result)) { echo "<table>"; echo "<tr valign='top'><td><b><a href='#".$row['Cat']."'>".$row['Cat']."</a></b><br>"; $sCatID = ($row['CatID']); $result2 = mysql_query("SELECT * FROM test_prefixSubCat WHERE CatID=$sCatID"); while($row2 = mysql_fetch_array($result2)) { $sSub = ($row2['CatID']); $sSubID = ($row2['SubID']); echo "<dd><a href='#'>".$row2['Sub']."</a><br>"; Had to rearrange a few things in it. Now its working, your answer helped thanks.
0
$result = ("SELECT * FROM `test_prefixCatagory` ORDER by `Cat`");

2 Comments

the "`" character is only necesary when tables or column names are sql keywords (values, relations, keys, rows, columns, order etc.).
yes its strongly necessary in case of keywords, but harmless at all
0

Not only do you need to add mysql_query but you also need to remove the single quotes from the table name and field name. You can use backticks if you wish but not single quotes around table names.

$result = mysql_query("SELECT * FROM `test_prefixCatagory` ORDER by `Cat`");

// other query:
 $result2 = mysql_query("SELECT * FROM `test_prefixSubCat` WHERE `CatID`=$sCatID");

When debugging MySQL problems, use mysql_error() to see a description of the problem.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.