The.Pr0fess0r Posted September 2, 2009 Share Posted September 2, 2009 Hello everyone, here is my problem... I am trying to pull some data from my database. Here is the query I am using: $query = "SELECT * FROM hunt_info WHERE huntStatus = $key AND username = $username ORDER BY date_of_hunt DESC"; When I use the above code, this is what I get in the table where the data is supposed to be: SELECT * FROM hunt_info WHERE huntStatus = 1 AND username = Bob ORDER BY date_of_hunt DESC; So, it looks like it is pulling the variables correctly (key = 1 and username = Bob), but i'm not sure why it isn't displaying the data and only showing the query code? As soon as I take username = $username out of my query statement everything will display properly again. Here is the code for the entire column of my table: <td> <form action="hunt_reports.php" method="post"> Search: <input type="text" name="search" value="<?=$search;?>"> <input type="submit" value="search"> (Enter Date, Name of Hunter, or Species of Animal) </form><br> <?PHP { $status_array = array('1' => 'Successful', '2' => 'Unsuccessful'); foreach ($status_array as $key => $value) { echo '<font size=+1 color=blue><i>' . $value . '</i></font><br>'; $query = "SELECT * FROM hunt_info WHERE huntStatus = $key AND username = $username ORDER BY date_of_hunt DESC"; $result = @mysql_query ($query) or die ("$query"); echo '<table cellspacing="0" cellpadding="0">'; $i=0; while ($row = mysql_fetch_array ($result)) { $hunter = $hunter[0]; if($i==0) { echo '<tr><td align="center"><font size="-1">Date</font></td><td></td>'; echo '<td align="center"><font size="-1">Hunter</font></td><td></td>'; echo '<td align="center"><font size="-1">Location</font></td><td></td>'; echo '<td align="center"><font size="-1">Wind Direction</font></td><td></td>'; echo '<td align="center"><font size="-1">Harvested (Qty)</font></td><td></td>'; echo '<td align="center"><font size="-1">Sighted</font></td><td></td>'; echo '<td align="center"><font size="-1">Username</font></td><td></td>'; } if(fmod($i,2)==0) { $color = "#DCDCDC"; } else { $color = "#FFFFFF"; } $i++; echo '<tr bgcolor="' . $color . '">'; echo '<td width="50" align="right">' . substr($row['date_of_hunt'],0,10) . '</td><td width="20"></td>'; echo '<td width="50" align="center">' . $row['hunter'] . '</td><td width="20"></td>'; echo '<td width="80" align="center">' . $row['location'] . '</td><td width="20"></td>'; echo '<td width="80" align="center">' . $row['wind_direction'] . '</td><td width="20"></td>'; echo '<td width="100" align="center" nowrap>' . $row['species1'] . ' ' . $row[('qty1')] . '<br>' . $row['species2'] . ' (' . $row['qty2'] . ')<br>' . $row['species3'] . ' (' . $row['qty3'] . ')<br>' . $row['species4'] . ' (' . $row['qty4'] . ')</td><td width="20"></td>'; echo '<td width="100" align="center" nowrap>' . $row['sighted1'] . ' (' . $row['sighted_qty1'] . ')<br>' . $row['sighted2'] . ' (' . $row['sighted_qty2'] . ')<br>' . $row['sighted3'] . ' (' . $row['sighted_qty3'] . ')<br>' . $row['sighted4'] . ' (' . $row['sighted_qty4'] . ')<br></td><td width="20"></td>'; echo '<td width="30" align="center">' . $row['username'] . '</td><td width="20"></td>'; } if($i==0) echo '<tr><td><i>None</i></td></tr>'; echo '</table><br><br>'; } echo '</form>'; } ?> </td> Thank you for any help you can give me! Quote Link to comment https://forums.phpfreaks.com/topic/172884-solved-problem-extracting-data-from-database/ Share on other sites More sharing options...
mikesta707 Posted September 2, 2009 Share Posted September 2, 2009 you must enclose strings with single quotes $query = "SELECT * FROM hunt_info WHERE huntStatus = $key AND username = '$username' ORDER BY date_of_hunt DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/172884-solved-problem-extracting-data-from-database/#findComment-911168 Share on other sites More sharing options...
rhodesa Posted September 2, 2009 Share Posted September 2, 2009 if you update this line: $result = @mysql_query ($query) or die ("MySQL Error: ".mysql_error()); it will give you info on what is wrong...to fix the problem, i think this is what you need: $query = "SELECT * FROM hunt_info WHERE huntStatus = '$key' AND username = '$username' ORDER BY date_of_hunt DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/172884-solved-problem-extracting-data-from-database/#findComment-911170 Share on other sites More sharing options...
The.Pr0fess0r Posted September 2, 2009 Author Share Posted September 2, 2009 Wow, that was fast! Thank you very much for helping me out. As you can tell i'm just learning PHP, but i'm learning more and more every day just by reading these forums! you must enclose strings with single quotes $query = "SELECT * FROM hunt_info WHERE huntStatus = $key AND username = '$username' ORDER BY date_of_hunt DESC"; Thanks again for your help! Quote Link to comment https://forums.phpfreaks.com/topic/172884-solved-problem-extracting-data-from-database/#findComment-911175 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.