Jump to content

My WHERE piece isn't working


silvrfoxx

Recommended Posts

I have been working on one of my sites updating it to php w/ mySQL and so I am pretty new and mostly self taught on the php part.

 

I am having trouble when I am running my script that I am getting the all of the information in my tables to display great, but the WHERE part of my mySQL script doesn't seem to be narrowing down the results, it is displaying everything.

 

Please help me figure out what I have wrong, plus any advise on things I am missing would help too.  Thanks.

 

<table>
<?
$result = mysql_query("SELECT * FROM table WHERE Genre = 'action'");
while ($result_rows = mysql_fetch_array($result))
{
?>
				<tr>
					<td align="center" valign="middle">
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><? echo $result_rows[Name] ?></a></p>
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><img src="images/<? echo $result_rows[image] ?>" alt="<? echo $result_rows[Name] ?>" width="70" height="70"></a></p>
					</td>
<?
$result_rows = mysql_fetch_array($result);
if ($result_rows > 0)
{
?>
					<td align="center" valign="middle">
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><? echo $result_rows[Name] ?></a></p>
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><img src="images/<? echo $result_rows[image] ?>" alt="<? echo $result_rows[Name] ?>" width="70" height="70"></a></p>
					</td>
<?
}
else
{
?>
					<td align="center" valign="middle"></td>
<?
}
$result_rows = mysql_fetch_array($result);
if ($result_rows > 0)
{
?>
					<td align="center" valign="middle">
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><? echo $result_rows[Name] ?></a></p>
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><img src="images/<? echo $result_rows[image] ?>" alt="<? echo $result_rows[Name] ?>" width="70" height="70"></a></p>
					</td>
<?
}
else
{
?>
					<td align="center" valign="middle"></td>
<?
}
$result_rows = mysql_fetch_array($result);
if ($result_rows > 0)
{
?>
					<td align="center" valign="middle">
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><? echo $result_rows[Name] ?></a></p>
						<p><a href="index.php?action=<? echo $result_rows[Link] ?>"><img src="images/<? echo $result_rows[image] ?>" alt="<? echo $result_rows[Name] ?>" width="70" height="70"></a></p>
					</td>
				</tr>
<?
}
else
{
?>
					<td align="center" valign="middle"></td>
				</tr>
<?
}
}
?>
</table>

Link to comment
https://forums.phpfreaks.com/topic/140179-my-where-piece-isnt-working/
Share on other sites

SELECT * FROM table WHERE Genre = 'action';

If you run this query in your mysql console or PHPmyadmin, does it return the expected results?

 

Also you are overwriting $result_rows each time, so it looks like it would be display the first row returned over and over?

When i run the query in PHPmyadmin it does return the expected results.

 

When the code is run it isn't displaying the first row over and over, it is displaying all of rows in the database.  Which is undesired because I am wanting to just narrow it down to those with the Genre of action.

 

The reason for the $result_rows over and over again is to reaccess the database for the next row.  I have to do this because I am wanting to display the page with a row of 4 items then go to the next row (the <tr> is at the beginning then there are 4 <td> and then the </tr>)

Well, if you say mysql is return this right result, then you should test the PHP is receiving...  Debug by looking at the first part of your script:

$result = mysql_query("SELECT * FROM table WHERE Genre = 'action'");
while ($result_rows = mysql_fetch_array($result))
{
  echo $result_rows[Name].'<br />';
}

And make sure that it returns the same data.  From there if it does, then it just comes down to an error displaying it.  You can probably use a modulus operator to make it echo a new row after 4 columns.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.