need help on get image data from database.

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!

Moderator: General Moderators

Post Reply
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

need help on get image data from database.

Post by lukelee »

On this page, it shows a list of images, when people click on a image, it will link to house.php, on house.php, there will be the details of the image people click.

Code: Select all

<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
while($row = mysql_fetch_assoc($query)) {
?>
<ul>
<li><a href="house.php?id=<?php echo $row['address']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
</ul>
<?php } ?>
here is house.php, it get the address from previouse page, and display all the images on this address.

Code: Select all

<?php 
 
require_once('db.php');[*][*]
$address = $_GET['address'];
query = mysql_query("SELECT * FROM image WHERE address = $address");
while($row = mysql_fetch_assoc($query)) {
?>
<ul>
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
</ul>
<?php } ?>
here is how my database looks like:

pid title address thumb imagedata

1 house carnegie 1 se234.jpg
2 farm carnegie 2 4hgh45.jpg
1 bed oakleigh 1 htgy4.jpg
1 hotel springvale 1 55fs.jpg


because each house may have more than 1 image, so I use address to identify different houses. thumb = 1 mean this image will be used as a thumbnail for people to click, thumb = 2 will be the images on house.php if people click the carnegie house, on the house.php, 'house' and 'farm' will be displayed on house.php

I wish people understand what i am doing. this looks simple, but I just couldnt get it work properly.
thanks for help
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: need help on get image data from database.

Post by andyhoneycutt »

You'll want to quote any strings you are using to perform lookups in your queries:

Code: Select all

query = mysql_query("SELECT * FROM image WHERE address = [color=#400000]'$address'[/color]");
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

You'll also want to put an opening anchor tag on each list item in house.php
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

chopsmith wrote:You'll also want to put an opening anchor tag on each list item in house.php
sorry, what is an opening tag? did i miss it?
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

It's just that on line 9 of house.php, you have:

Code: Select all

 
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
 
whereas you probably want:

Code: Select all

 
<li><a href='file_name_here'><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
 
or:

Code: Select all

 
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></li>
 
One more thing: not to be rude, but I absolutely despise the way you are going in and out of php. Starting a while loop, leaving php, resuming the code within the while loop, leaving php, then closing out the while loop. Also, I don't think this will ever output more than a single <li>.
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

chopsmith wrote:It's just that on line 9 of house.php, you have:

Code: Select all

 
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
 
whereas you probably want:

Code: Select all

 
<li><a href='file_name_here'><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
 
or:

Code: Select all

 
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></li>
 
One more thing: not to be rude, but I absolutely despise the way you are going in and out of php. Starting a while loop, leaving php, resuming the code within the while loop, leaving php, then closing out the while loop. Also, I don't think this will ever output more than a single <li>.

ah, yeah, i see. I have deleted </a>, but its still not working.
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

Change this:

Code: Select all

 
<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
while($row = mysql_fetch_assoc($query)) {
?>
<ul>
<li><a href="house.php?id=<?php echo $row['address']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
</ul>
<?php } ?>
 
To this:

Code: Select all

 
<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
echo "<ul>";
while($row = mysql_fetch_assoc($query)) {
  echo '<li><a href="house.php?id={$row['address']}"><img src="upload/{$row['imagedata']}" width="150" height="150" border="1" /></a></li>';
}
echo "</ul>";
 
Change this:

Code: Select all

 
<?php
require_once('db.php');[*][*]
$address = $_GET['address'];
query = mysql_query("SELECT * FROM image WHERE address = $address");
while($row = mysql_fetch_assoc($query)) {
?>
<ul>
<li><img src="upload/<?php echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
</ul>
<?php } ?>
 
To this:

Code: Select all

 
<?php
require_once('db.php');
$address = $_GET['address'];
query = mysql_query("SELECT * FROM image WHERE address = '{$address}'");
echo "<ul>";
while($row = mysql_fetch_assoc($query)) {
  echo '<li><img src="upload/{$row['imagedata']} width="150" height="150" border="1" /></a></li>';
}
echo "</ul>";
 
I'm pretty sure that's all you need. If it doesn't work, please let us know and repost the exact code you tried to run.
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

thanks chopsmith. but I still cant get it work, here is the codes:

Code: Select all

<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
echo "<ul>";
while($row = mysql_fetch_assoc($query)) {
  echo "<li><a href="house.php?id={$row['address']}"><img src="upload/{$row['imagedata']}" width="150" height="150" border="1" /></a></li>";
}
echo "</ul>";
?>

Code: Select all

 
<?php
require_once('db.php');
$address = $_GET['address'];
query = mysql_query("SELECT * FROM image WHERE address = '{$address}'");
echo "<ul>";
while($row = mysql_fetch_assoc($query)) {
  echo '<li><img src="upload/{$row['imagedata']} width="150" height="150" border="1" /></a></li>';
}
echo "</ul>";
?>
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

Are you getting any error messages or anything? What happens when you try to run it? If you are using some external db.php class/library, why are you using mysql_query()? When you use require_once(), no code is executed (I believe). You have to actually connect to the database before attempting to run a query. Perhaps that's the problem . . . Can you post the contents of db.php (of course, obfuscating any authentication information)?
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

chopsmith wrote:Are you getting any error messages or anything? What happens when you try to run it? If you are using some external db.php class/library, why are you using mysql_query()? When you use require_once(), no code is executed (I believe). You have to actually connect to the database before attempting to run a query. Perhaps that's the problem . . . Can you post the contents of db.php (of course, obfuscating any authentication information)?
there is no error message, both 2 pages show nothing on screen.
here is the db.php:

Code: Select all

<?php
$dbhost = 'localhost';
$dbuser = ' ';
$dbpass = ' ';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die                      ('Error connecting to mysql');
$dbname = ' ';
mysql_select_db($dbname);
?>
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

Yeah, you're never actually connecting to the database in the scripts. I'm heading out, so can't finish this up now, but I'll check it out tomorrow if it's not solved by then. Put a die() statement after the mysql_query() and you'll see that it's not executing. Also, put some debugging output in there and I'm sure you'll get it solved. Also, the lines with the <li>'s that I showed you might contain syntax errors (might have to escape some stuff). Good luck . . .
User avatar
chopsmith
Forum Commoner
Posts: 56
Joined: Thu Nov 13, 2008 10:40 am
Location: Red Bank, NJ, USA

Re: need help on get image data from database.

Post by chopsmith »

Also, for a quick test, copy and past the code from db.php into the top of each of the other files (and delete the require_once line) and then try to run it.
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

chopsmith wrote:Also, for a quick test, copy and past the code from db.php into the top of each of the other files (and delete the require_once line) and then try to run it.
no, still not working. acually the first page index.php is working if i write the codes:

Code: Select all

<?php
require_once('db.php');
$query = mysql_query("SELECT * FROM image WHERE thumb = '1'");
while($row = mysql_fetch_assoc($query)) {
?>
<ul>
<li><a href="house.php?id=<?php echo $row['address']; ?>"><img src="upload/<?PHP echo $row['imagedata']; ?>" width="150" height="150" border="1" /></a></li>
</ul>
<?php } ?>
so I am thinking if the address didnt passed to house.php or My sql code was wrong in house.php

and I tryed this in house.php

Code: Select all

 
<?php  
require_once('db.php');
$address=$_GET['address'];
echo "this is $address!";
?>
 
the output is this is ! seems the address wasnt been passed to house.php
lukelee
Forum Commoner
Posts: 28
Joined: Wed Sep 10, 2008 2:03 am

Re: need help on get image data from database.

Post by lukelee »

done it, made a very stupid mistake. thanks for help guys.
Post Reply