runei Posted October 9, 2008 Share Posted October 9, 2008 Hello. Can someone explain the difference of the following 2 querys? The first one works, sort of. I get all the id's in the table but only if i use an alias. The second one i dont get. I have defined the id as a variable of id. I only get a blank page. I know the code is wrong but could someone be helpful and explain the "correct" way to do it? Thx a bunch in advance. runei <?php include "../globals.php"; $query = "SELECT id AS LOK FROM forum_replies"; $result = mysql_query($query); $number = mysql_num_rows($result); if ($number == 0) { echo "no output"; }else{ echo '<table border ="1" align="center" cellspacing="3" cellpadding="3" width="75%"> #<tr><td align="left"><b>blahblah</b></td>'; while ($row = mysql_fetch_array($result)) { echo '<tr><td align="left">' . $row['LOK'] . '</td></tr> '; } echo '</table>'; } ?> <?php include "../globals.php"; $id = ($_GET['id']); if($id){ $query = "SELECT * FROM forum_replies WHERE id='".$id['id']."'"; $result = mysql_query($query); $number = mysql_num_rows($result); if ($number == 0) { echo "no output"; }else{ echo '<table border ="1" align="center" cellspacing="3" cellpadding="3" width="75%"> <tr><td align="left"><b>blahblah</b></td>'; while ($row = mysql_fetch_array($result)) { echo '<tr><td align="left">' . $row['id'] . '</td></tr> '; } echo '</table>'; } } ?> Link to comment https://forums.phpfreaks.com/topic/127745-solved-query/ Share on other sites More sharing options...
DarkWater Posted October 9, 2008 Share Posted October 9, 2008 It's because $id isn't an array, so therefore it does not have $id['id'] set...why would it? <?php $query = "SELECT * FROM forum_replies WHERE id = $id"; Also you should make sure $id is a number and typecast it to an INT to prevent SQL injections. Link to comment https://forums.phpfreaks.com/topic/127745-solved-query/#findComment-661126 Share on other sites More sharing options...
runei Posted October 9, 2008 Author Share Posted October 9, 2008 Thx. I'll put that one on my own account for brainfade Link to comment https://forums.phpfreaks.com/topic/127745-solved-query/#findComment-661135 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.