Forum Moderators: coopster

Message Too Old, No Replies

MySQL - Select Specific Row Based on Unique ID

beginners question

         

aloha4029

6:23 pm on Mar 7, 2007 (gmt 0)

10+ Year Member



I am building a photography website in html, and have a pre-formated table in which i want to put a thumbnail pic, the date of the pic, the name, and links to different sizes of the image. In a MYSQL database i have a table with four columns: ID (INT, auto-increment), ImageName (VARCHAR), FileName (VARCHAR), Date (VARCHAR).

I'm just getting started with PHP, and i want to either:

have the information automatically inserted into my pre-made table via a php loop command

or

write a script which will allow me to only enter the id# and will subsequently insert the name and the date into my table, and which might display the image of the filename.

I hope this is clear enough, I've bought quite a large book on PHP and have been browsing forums in an attempt to learn, but i can't find an example that inserts it into a pre-existing table. I appreciate any help.

- nick

cameraman

6:54 pm on Mar 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld aloha4029! And welcome to the greatest language devised by mortal man.
There's at least 8 different ways to do this. Here's one:

<?php
// Assuming that you've opened a db connection above
$qry = mysql_query("SELECT * FROM sometable ORDER BY ID");
?>
<table>
<tr><th>id</th><th>image name</th><th>file name</th><th>Date</th></tr>
<?php
while($dat = mysql_fetch_assoc($qry)) {
echo "<tr>\n";
echo '<td>' . $dat['ID'] . "</td>\n";
echo '<td>' . $dat['ImageName'] . "</td>\n";
echo '<td><a href="' . $dat['FileName'] . '">' . $dat['FileName'] . "</a></td>\n";
echo '<td>' . $dat['Date'] . "</td>\n";
echo "</tr>\n";
}// EndWhile getting data
mysql_free_result($qry);
?>
</table>

Basically, you use the echo function wherever you want to send stuff to the browser. I echo'd the lines in completeness, but you can also do it this way:

<?php while($dat = mysql_fetch_assoc($qry)) {?>
<tr>
<td><?php echo $dat['ID'];?></td>
</tr>
<?php }// EndWhile getting data?>

You'll notice that the filename is a clickable link, which may not be what you want. You could just as easily make it go to a formatted picture display page with something like
<a href="picturepage.php?id=" . $dat['ID'] . '">'

You may find that storing dates as timestamps (using BIG INT as data type) is a lot more versatile. The time() function is one of several that gives you this timestamp. You get it back into human readable form with the date() function.