PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This is a little tricky to explain but I'll do my best.
For reasons that I won't go into right now (for fear of boring you all to death) I have written the following code which interrogates a table named "photos" and uses a filename to call a second PHP page.
$query = "SELECT * FROM photos WHERE code='$scode'";
$result = mysql_query($query)
or die ("Couldn't execute query.");
if (mysql_num_rows($result) == 0)
{echo "<tr><td class='photolink' $al colspan='2'> </td></tr>";}
else
{
while($row = mysql_fetch_assoc($result))
$filename = $row['filename'];
echo "<tr><td class='photolink' $al colspan='2'><a href='gallery.php?category=$catsel&scode=$scode&autostart=$filename'><img src='../graphics/services/gallery.gif' title='Click here to view our $service image gallery!' alt=''></img></a></td></tr>";}
Here's the clincher. The $scode array has multiple instances through the table.
What I WANT the code to do is find the first instance of $scode (i.e. the first row where code = $scode) and use THAT filename. As it stand the code works fine but it sets $filename as the LAST instance of $scode that matches.
append LIMIT 1 to the SQL statement for using without a loop
kdidymus wrote:What I WANT the code to do is find the first instance of $scode (i.e. the first row where code = $scode) and use THAT filename. As it stand the code works fine but it sets $filename as the LAST instance of $scode that matches.
use ORDER BY to get really first instance. For example, ORDER BY id
SQL works with set of data but not with lists
Thank you both. Not only did your suggestions work to solve the problem but I now understand that I can use mysql_fetch_array OUTSIDE of a loop. It makes perfect sense now.