i have writeen following query in my code.

$tquery="SELECT questions.id,questions.date,questions.title,users.user_name,subjects.subject FROM questions INNER JOIN users ON questions.student_id=users.id INNER JOIN topics ON questions.topic_id=topics.id INNER JOIN subjects ON topics.subject_id=subjects.id WHERE subjects.id=".$_SESSION['sub1']." OR subjects.id=".$_SESSION['sub2'].";"; 

but when i am trying to echoing its result it is not displaying anything.

Recommended Answers

All 4 Replies

1) semicolor at the end is not needed

or

$tquery="SELECT questions.id,questions.date,questions.title,users.user_name,subjects.subject FROM questions INNER JOIN users ON questions.student_id=users.id INNER JOIN topics ON questions.topic_id=topics.id INNER JOIN subjects ON topics.subject_id=subjects.id WHERE subjects.id=".$_SESSION['sub1']." OR subjects.id=".$_SESSION['sub2'].";"; 

echo $tquery;

2)
a you can echo query before mysql_query,

b run script in browser,
c you will find prepared query on browser output
d copy the output query
e open phpmyadmin or any other interface for mysql
f run that copied prepared query and see query give result or not, otherwise make changes accordingly

the copied prepared query is giving result.but the code is not displaying on the browser.my whole page code is-

<?php SESSION_START(); ?>

<link rel='stylesheet' href='style_sheet.css' type='text/css' media='screen' />
<?php
require 'config.php';
if($_SESSION['user_type']=='student')
{
$query="SELECT * FROM questions WHERE student_id='".$_SESSION['id']."';";
$result=mysql_query($query) or die(mysql_error());

?>
<a href='new_question.php' class='new_topic'>New Question</a>
<table class="index_table">
<tr>
<td colspan="8">Old Questions</td>
<td colspan="8">Date Posted</td>
</tr>
<?php while($rows=mysql_fetch_assoc($result)) { ?>
<tr>
<td colspan="8"><a href="view_question.php?id=<?php echo $rows['id']; ?>" > <?php echo $rows['title']; ?> </a></td><td colspan="2"><?php echo $rows['date'];?></td>
</tr>
<?php } ?>
</table>
<?php
}
IF($_SESSION['user_type']=='teacher'){ ?>
    <h3> Questions Posted</h3>
<?php 

$tquery="SELECT questions.id,questions.date,questions.title,users.user_name,subjects.subject FROM questions INNER JOIN users ON questions.student_id=users.id INNER JOIN topics ON questions.topic_id=topics.id INNER JOIN subjects ON topics.subject_id=subjects.id WHERE subjects.id=".$_SESSION['sub1']." OR subjects.id=".$_SESSION['sub2']; 


$result=mysql_query($tquery) or die(mysql_error());
?>
<table>
<?
While($trow=mysql_fetch_assoc()){ ?>

       <tr>
      <td><a href="view_question.php?id=<? echo $trow['id'];?>"><h3><? echo $trow['title'];?></h3></a></td>
       </tr>
       <tr>
      <td>by&nbsp;<? echo $trow['user_name']; ?>&nbsp;in <?echo $trow['subject'];?>&nbsp;on<? echo $trow['date']; ?></td>
</tr>

     <?php }
?>
  </table>
   <?php }
?>

change line 34-37 to

$tresult=mysql_query($tquery) or die(mysql_error());
?>
<table>
<?
While($trow=mysql_fetch_assoc($tresult) { ?>

I have added $tresult in paramenter, do not use $result again

thank you for help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.