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
https://forums.phpfreaks.com/topic/208360-help-with-this-code/
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
https://forums.phpfreaks.com/topic/208360-help-with-this-code/#findComment-1088885
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.