1

Any idea why this is showing a result of null? I'm guessing it has to do with where I put $link before I did the query, but it has to be done that way, correct?

function getcatposts($cat_id) {
$qc = @mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
  $topic_title=$row['topic_subject'];
  $topic_id=$row['topic_id'];
}
$qc2 = @mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
  $cat_name=$row2['cat_name'];
}

Updated code:

function getcatposts($cat_id) {
$link = mysqli_connect("localhost", "lunar_lunar", "", "lunar_users");
$qc = mysqli_query($link, "SELECT * FROM topics WHERE topic_cat='$cat_id'");
while($row = mysqli_fetch_array($qc)) {
  $topic_title=$row['topic_subject'];
  $topic_id=$row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
  $cat_name=$row2['cat_name'];
}
echo $cat_name;
echo '<br />';
echo $topic_title;
echo '<br />';
echo $topic_id;
}

New issue is that its displaying like this: http://gyazo.com/43e8a91b9e0cf4f5e413536907891dcf.png When the DB looks like this: http://gyazo.com/1ead8bd0f150838dae3ee4a476419679.png

It should be displaying all three of them and this is a function meaning it will keep redoing all the code until it can't query anymore data. Any ideas?

5
  • 2
    To find out, remove the error suppression operator (@) and change your statement to $qc = mysql_query(...) or die(mysql_error());. Also, do the $link isn't accessible inside your function scope, and you'll have to pass it as another function parameter. Doing a var_dump($link); inside your function would have told you that. Commented Oct 12, 2013 at 4:51
  • Smells like a SQL Injection here. Commented Oct 12, 2013 at 4:52
  • It ended up being due to the fact that I declared $link outside of the function. Although now its not working properly. Its only displaying it once when it should display three things. I will update the post. Commented Oct 12, 2013 at 4:53
  • How do you know that it's returning null? can you show us the whole function? Commented Oct 12, 2013 at 4:53
  • It was returning null because that was the error message. Commented Oct 12, 2013 at 4:55

2 Answers 2

1

The problem here is that you're trying to echo the values outside your loop. The variables inside the loop will get overwritten on each iteration and at the end of looping, the variable will hold the value of the last iteration.

If you want to display all the values, move the echo statement inside your loop, like so:

while($row = mysqli_fetch_array($qc)) 
{
    $topic_title = $row['topic_subject'];
    $topic_id = $row['topic_id'];
    echo $topic_title.'<br/>';
    echo $topic_id.'<br/>';
}

$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");

while($row2 = mysqli_fetch_array($qc2)) 
{
    $cat_name = $row2['cat_name'];
    echo $cat_name.'<br/>';
}

If you care about the order, you could store the titles, ids and cat_names in arrays like so:

while($row = mysqli_fetch_array($qc)) 
{
    $topic_title[] =$row['topic_subject'];
    $topic_id[] = $row['topic_id'];
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) 
{
    $cat_name[] =$row2['cat_name'];
}

And then loop through them:

for ($i=0; $i < count($topic_id); $i++) { 
    if( isset($topic_id[$i], $topic_title[$i], $cat_name[$i]) )
    {
        echo $cat_name[$i].'<br/>';
        echo $topic_title[$i].'<br/>';
        echo $topic_id[$i].'<br/>';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Your function displays only one result because your echo is outside the while loop... Put the Echo statements inside the loop, or you will print only the last result!

while($row = mysqli_fetch_array($qc)) {
  $topic_title=$row['topic_subject'];
  $topic_id=$row['topic_id'];
  echo $topic_title;
  echo '<br />';
  echo $topic_id;
}
$qc2 = mysqli_query($link, "SELECT * FROM categories WHERE cat_id='$cat_id'");
while($row2 = mysqli_fetch_array($qc2)) {
  $cat_name=$row2['cat_name'];
  echo $cat_name;
  echo '<br />';
}

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.