Jump to content

Help with this code


lalnfl

Recommended Posts

 

This is just a piece of coding, hopefully you can understand what I am trying to get and help me get it:

 

$sql = mysql_query("SELECT * FROM Topic WHERE category_id='$category_id' ORDER BY id DESC LIMIT 10");

while ($row = mysql_fetch_array($sql)){

$result = mysql_num_rows($sql);

 

$topic_id = $row['id'];

$topic_name = $row['topic_name'];

$posts = $row['posts'];

$mem_id = $row['mem_id'];

 

 

$get_mem_name = mysql_query("SELECT * FROM Member WHERE id='$mem_id'");

$get_mem_row = mysql_fetch_array($get_mem_name);

 

$mem_name = $get_mem_row['username'];

 

$get_reply_mem = mysql_query("SELECT * FROM Reply WHERE topic_id='$topic_id'");

$get_results = mysql_num_rows($get_reply_mem);

 

if ($get_results >= 1){

 

$test = "works";

 

}

 

else {

 

$test = "doesn't work";

 

}

 

 

Explain to me why the above coding is getting the else statement "doesn't work" instead of the if statement "works" and tell me what I should do different to get what I am looking for

 

 

 

 

 

Link to comment
Share on other sites

   

$sql = mysql_query("SELECT * FROM `Topic` WHERE `category_id`='{$category_id}' ORDER BY `id` DESC LIMIT 10")or die(mysql_error()); //remove or die() after testing

$num = mysql_num_rows($sql);

if($num ==0){
echo 'No results';
die();
}

   while ($row = mysql_fetch_assoc($sql)){
   
   $topic_id = $row['id'];
   $topic_name = $row['topic_name'];
   $posts = $row['posts'];
   $mem_id = $row['mem_id'];
      
   $get_mem_name = mysql_query("SELECT * FROM `Member` WHERE `id`='{$mem_id}' ");
   $get_mem_row = mysql_fetch_assoc($get_mem_name);
   
   $mem_name = $get_mem_row['username'];
   
   $get_reply_mem = mysql_query("SELECT * FROM `Reply` WHERE `topic_id`='{$topic_id}' ");
   $get_results = mysql_num_rows($get_reply_mem);
   
   if ($get_results >= 1){
   
   $test = "works";
   
   }else{
    $test = "doesn't work";
   }
echo $test."<br>";
}//end while

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.