Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am currently creating a MySQLi PHP forum, and i'm having some problems with the MySQL LEFT JOINs, i am trying to get all the subforums "joined" with the set catid,

the query itself works fine when testing it in mysql workbench, and i receive the correct list. but whenever i try to put the results into an array, i get null

My current codelooks like this

$sql = 'SELECT `c`.`id`, `c`.`name`, `sc`.`id_sub`, `sc`.`name_sub`, `sc`.`catid` 
    FROM `forum_categories` AS `c` 
    LEFT JOIN `forum_subcategories` AS `sc` 
    ON `c`.`id` = `sc`.`catid`';
$stmt = $mysqli->query($sql);
$forum_categories = array();
while($row = $stmt->fetch_array(FETCH_ASSOC))
{
   // define forum category
$forum_categories[$row['id']] = array(
    'title' => $row['name'],
);

    // add forums to category
$forum_categories[$row['id']]['forum_subcategories'][] = array(
    'id' => $row['id_sub'],
    'title' => $row['name_sub']
);
}

var_dump ($row->name);

But the var_dump just returns null.

This is what the var_dump($stmt); returns

object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(5) ["lengths"]=> NULL ["num_rows"]=> int(10) ["type"]=> int(0) }
share|improve this question
    
move var_dump just above closing } –  Lashane Nov 3 '13 at 22:12
1  
why you're dumping $row->name instead of categories? –  Your Common Sense Nov 3 '13 at 22:17

2 Answers 2

Mysqli never interfere with your queries.

If fetch_array returned null, then there are no rows left in resultset.

share|improve this answer
    
But when running the query in mysqlworkbench, i get the correct result? –  Daniel Holst Nov 4 '13 at 8:17
    
well you are running your workbench against other database, or query is not the same. Or you actually get correct result from mysqli either but for some reason don't use it. –  Your Common Sense Nov 4 '13 at 8:20

This is because var_dump() is outside the while loop. As while exits when fetch_array returns false, the value of $row is false at the end of loop.

share|improve this answer
    
Why -1 ??? some people should get banned for giving -1 without reason. Or for not giving the reason in comment as it is said in rules of StackOverflow. –  Flash Thunder Nov 3 '13 at 22:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.