Re: Problems Creating the Code to Open Images Within a Template PHP Page
- From: Jerry Stuckle <jstucklex@xxxxxxxxxxxxx>
- Date: Fri, 28 Apr 2006 07:21:36 -0400
ste wrote:
Hi there,
I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I wondered if someone could help me out with a problem I'm having, or at least point me in the right direction? I have setup a MySQL database which contains the following, though I would like to expand on this in the future with lots of extra fields:
IMAGES TABLE:
-------------------
imageid (primary key)
imagelocation (url location for image)
imagecaption (caption for image, not used in the code below, but will be used once I suss this out!)
What I want to do is, from a HTML gallery of thumbnails, be able to open a larger version of each thumbnail image in a nice pretty formatted HTML page. Each HTML page would be identical, so that's why I only want to create this once, as opposed to hard coding them all. As I want to keep this simple and one step at a time, I'm prepared to create the image gallery and the appropriate image URL's.
My problem is that I'm unsure of the php/mysql I need to write in order to open the appropriate picture in the image template. For example, if I hover over and click 'image 1,' I would like the browser to open the page www.mywebsite.com/imagetemplate.php?id=1 This would open the image template page with image 1 visible within it.
So when executing the sql query which selects a particular image from the database, I would like it to find the record which has an ID equal to the ID in the URL above (in this case, 1).
Now what I've tried isn't working as I've got it all wrong, but here it is for interest:
DODGY CODE:
------------------
<?php
/* this is the include file for my database passwords */
include("my_db_login.inc");
/* this is the code to make the connection to the database */
$connection = mysql_connect($host,$user,$password) or die ("couldn't connect to server");
$db = mysql_select_db($database,$connection) or die ("Couldn't select database");
/* this query SHOULD be requesting all records from My Database where the ImageID is equal to the ImageID listed in the URL as mentioned above */
$query = "SELECT * FROM my_database WHERE imageid = \"{$_POST['imageid']}\"";
$result = mysql_query($query) or die ("Couldn't execute query.");
/* the line of code below is actually the code from the table cell which SHOULD insert the image location url for the image where the id is equal to the one requested in the url above */
echo "<img src=\"{$row['imagelocation']}\" width=\"500\" border=\"0\" />";
?>
I'd be grateful for any help with this as I'm obviously doing something (or many things!) wrong.
Thanks,
Ste
You're close. mysql_query() returns a result object; you need to retrieve the actual data (into $row in your example).
After the mysql_query() add the following:
$row = mysql_fetch_array($result);
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================
.
- Prev by Date: Re: Warning: ereg(): REG_BADBR
- Next by Date: Re: How can I open a file in the cgi-bin folder without getting a 404 error?
- Previous by thread: Looking for general advice on security
- Next by thread: Re: Problems Creating the Code to Open Images Within a Template PHP Page
- Index(es):
Relevant Pages
|