mysqli query fails, no errors

mysql, mysqli, php

Solution

The display_children() function doesn't have access to the $connect variable.

Try this:

function display_children($parent) {
    global $connect;
    $query = "SELECT categoryId, categoryName FROM `categories` WHERE parentCategory=".$parent;
    $result = mysqli_query($connect,$query)
        or die ("Couldn't execute query: ".mysqli_error());

    echo "<ul>";
    while ($row = mysqli_fetch_assoc($result)) {
          echo "<li>".$row['categoryName']."</li>";
          display_children($row['categoryId']); 
    }
    echo "</ul>";
    mysqli_close($connect);
}

Problem

I have a database, called 'catalog', and a table, called 'categories'. The table has 3 columns in this order: categoryId, categoryName, parentCategory. I'm trying to grab categoryId and categoryName for each row that has a parentCategory = 'root'. I thought it was a straightforward query, but I'm apparently doing something wrong, because I keep getting the message--Couldn't execute query--but no mysql error is being displayed. I've posted my code below. Can anyone point me straight? P.S. I do have values assigned to the $db variables; I just didn't include those here. ``` <?php $connect = mysqli_connect($db_host,$db_user,$db_password,$db_database) or die ("Couldn't connect to server: ".mysqli_error()); function display_children($parent) { $query = "SELECT categoryId, categoryName FROM `categories` WHERE parentCategory=".$parent; $result = mysqli_query($connect,$query) or die ("Couldn't execute query: ".mysqli_error()); echo "<ul>"; while ($row = mysqli_fetch_assoc($result)) { echo "<li>".$row['categoryName']."</li>"; display_children($row['categoryId']); } echo "</ul>"; mysqli_close($connect); } ?> <div class="menu"> <?php /* Menu Write */ display_children("root"); ?> </div> ```

Original source

Related problems