june_c21 Posted May 5, 2008 Share Posted May 5, 2008 i don't understand why this code can get exactly the same output from database. Did i miss out something ?? Pls help and guide me. thanks in advance the sql statement are correct when i run in phpmyadmin but why when display in IE , it came out same output and not according the one i try in phpmyadmin if ($GF = "1" && $Year = "2006") { $query = "SELECT reg_no, title1, incident_date, fault_rep_id, secr_recd, status FROM report WHERE GF = '1'AND year = '2006' "; $result = mysql_query($query, $dblink); while($myrow = mysql_fetch_row($result)) { echo "<tr><td align=center><span class=style2>" . $myrow[0] . "</span</td><td align=center><span class=style2>" . $myrow[1] . "</span</td><td align=center><span class=style2>" . $myrow[2] . "</td></span><td><span class=style2>" . $myrow[3] . "</span></td><td align=center><span class=style2>" . $myrow[4] . "</span></td><td align=center><span class=style2>" . $myrow[5] . "</span></td></tr>"; } } if ($GF = "2" && $Year = "2006") { $query = "SELECT reg_no,title1,incident_date,fault_rep_id,secr_recd,status from report where GF='2' AND year = '2006' "; $result = mysql_query($query, $dblink); while($myrow = mysql_fetch_row($result)) { echo "<tr><td align=center><span class=style2>" . $myrow[0] . "</span</td><td align=center><span class=style2>" . $myrow[1] . "</span</td><td align=center><span class=style2>" . $myrow[2] . "</td></span><td><span class=style2>" . $myrow[3] . "</span></td><td align=center><span class=style2>" . $myrow[4] . "</span></td><td align=center><span class=style2>" . $myrow[5] . "</span></td></tr>"; } } Quote Link to comment Share on other sites More sharing options...
trq Posted May 5, 2008 Share Posted May 5, 2008 The greater question is why are you running two separate queries in the first place? Quote Link to comment Share on other sites More sharing options...
june_c21 Posted May 5, 2008 Author Share Posted May 5, 2008 i want when user click on GF1 && Year 2006 , it will display only result from GF1 and Year 2006.That is the reason why i seperate it. Quote Link to comment Share on other sites More sharing options...
trq Posted May 5, 2008 Share Posted May 5, 2008 Ah, now I see. Hard to read your code with its lack of indentation. Where do you define $GF and $Year? Also note that = is an assignment operator not comparison. You need == Its likely your if should look something like.... if ($_GET['GF'] == 1 && $_GET['Year'] == 2006) { Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 5, 2008 Share Posted May 5, 2008 Another good question is why there is not error handling in that script, such as: $result = mysql_query($query, $dblink) or die ("Query:<br />$query<br />Error:<br />".mysql_error()); Quote Link to comment Share on other sites More sharing options...
june_c21 Posted May 5, 2008 Author Share Posted May 5, 2008 Ah, now I see. Hard to read your code with its lack of indentation. Where do you define $GF and $Year? Also note that = is an assignment operator not comparison. You need == Its likely your if should look something like.... if ($_GET['GF'] == 1 && $_GET['Year'] == 2006) { i still having problem to display the result. it came out not according to what i choose (user input) Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 6, 2008 Share Posted May 6, 2008 i still having problem to display the result. it came out not according to what i choose (user input) And what have you tried to debug the issue? Did you echo the query to the page to verify that the query is what you expect? Are there any results or just incorrect results? We have no idea what is in your database and what you expect to be returned. You never stated that the two values are supposed to come from the GET array, that was an assumption that thorpe made. Is that the case? Where are $GF and $Year supposed to be defined? In any event the logic in your code is horribly inefficient. As thorpe already stated having two IF statements makes no sense sice you are just using those values in the query anyway. The only prupose would be for validating that the values are within the proper range. And, if that's the case you only need one IF statement. You also don't need to have two sets of code for displaying the results. This won't "fix" whatevr the issue is with not gettng the correct results, but it will clean it up. But, I did add some debuggin code to help trace the problem. I also noticed that the code to write the results had some erros as well <?php //IF statement used just to validate the range of the values if ($Year = "2006" && ($GF = "1" || $GF = "2")) { $query = "SELECT reg_no, title1, incident_date, fault_rep_id, secr_recd, status FROM report WHERE GF = '" . $GF . "'AND year = '" . $Year . "'"; $result = mysql_query($query, $dblink); if ($result==false) { //Error with Query echo "The query:<br />$query<br />Produced the error:<br />".mysql_error()); } else if (mysql_num_rows($result)==0) { //No results echo "The query:<br />$query<br />Returned 0 records."; } else { //Query was successful & there were results while($myrow = mysql_fetch_assoc($result)) { echo "<tr>"; echo "<td align=\"center\"><span class=\"style2\">" . $myrow['reg_no'] . "</span></td>"; echo "<td align=\"center\"><span class=\"style2\">" . $myrow['title1'] . "</span></td>"; echo "<td align=\"center\"><span class=\"style2\">" . $myrow['incident_date'] . "</span></td>"; echo "<td><span class=\"style2\">" . $myrow['fault_rep_id'] . "</span></td>"; echo "<td align=\"center\"><span class=\"style2\">" . $myrow['secr_recd'] . "</span></td>"; echo "<td align=\"center\"><span class=\"style2\">" . $myrow['status'] . "</span></td>"; echo "</tr>"; } } } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.