Php code dont return data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
williamh262000
Forum Newbie
Posts: 2
Joined: Sat Nov 12, 2011 5:45 pm

Php code dont return data

Post by williamh262000 »

Hi, I have the following tables:
topic
topic_id int(10) AUTO_INCREMENT
sub_id int(10)
topic
problems
problem_id int(10) AUTO_INCREMENT
topic_id int(10)
problem
THIS IS MY CODE TO GET THE PROBLEMS FROM EACH TOPIC

Code: Select all

<?php
 login();
         $topic_id = $_GET['cat'];
         $query="SELECT  problem_id, problem from problems WHERE topic_id=$topic_id ORDER BY problem ASC";
         $result=mysql_query($query);
         while($row=mysql_fetch_array($result,MYSQL_ASSOC))
         {

                  $problem_id = $row['problem_id'];
                  $problem = $row['problem'];

             echo "<h3><a href=\"index.php?content=answers&cat=$topic_id\">$problem</a></h3><br>\n";
         }
?>
But returns repeadeatly the same problems in each topic.... how i can fix it. ... thanks for you cooperation
Last edited by Benjamin on Mon Nov 14, 2011 10:56 pm, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
danwguy
Forum Contributor
Posts: 256
Joined: Wed Nov 17, 2010 1:09 pm
Location: San Diego, CA

Re: Php code dont return data

Post by danwguy »

don't assign it to a var in the while statement if you are going to echo it out at the same time also I always surround my queries with single quotes when using =, just use

Code: Select all

$topic_id = $_GET['cat'];
$query="SELECT problem_id, problem from problems WHERE topic_id = '$topic_id' ORDER BY problem ASC";
$result=mysql_query($query);
while($row=mysql_fetch_assoc($result))
{

echo "<h3><a href='index.php?content=answers&cat=".$topic_id."'>".$row['problem']."</a></h3><br>\n";
}
?>
williamh262000
Forum Newbie
Posts: 2
Joined: Sat Nov 12, 2011 5:45 pm

Re: Php code dont return data

Post by williamh262000 »

still does not work... it returns always the same problem from the topics...

Code: Select all

http://localhost/chemath.com/index.php?content=topics&cat=1
the cat=1 is always return for the topic 1, topic 2 ....etc

when it suppose to be cat=2 for topic 2... etc
Post Reply