Jump to content

Recommended Posts

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!

 

you must enclose strings with single quotes

 

$query = "SELECT * FROM hunt_info WHERE huntStatus = $key AND
                            username = '$username'
                            ORDER BY date_of_hunt DESC";

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";

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.