kikilahooch Posted April 21, 2006 Share Posted April 21, 2006 I'm trying to display images from a database that are the result of a search. I am able to get an image to display but the problem is it keeps on showing the same image everytime. Here is my code:[code]$sql="select * from product";$sql ="select image, prodName ,price from product where dept = 'ladies'";//db$result = mysql_query($sql,$conn) or die(mysql_error());//get the number of rows in the result set; should be 1 if a matchif (mysql_num_rows($result) >= 1) { //if authorized, get the values of name, surname $image = mysql_result($result, 0, 'image'); $prodName = mysql_result($result, 0, 'prodName'); $price = mysql_result($result, 0, 'price');}$result = @mysql_query($query); if($result){ echo' <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1> <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8"> <tr> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td> </tr>'; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo'<tr> <td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td><td align="center"><b>'.$row['prodName'].'</td></b><td align="center"><b>€'.$row['price'].'</td></b> </tr>'; } echo'</table>'; } else{ echo'<h1> System Error </h1> table '; exit(); } mysql_close();?>[/code]Any idea why its displaying the same image everytime?? Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/ Share on other sites More sharing options...
SharkBait Posted April 21, 2006 Share Posted April 21, 2006 I think you'll need a while loop in there[code]$result = mysql_query($sql, $conn) or die(mysql_error());//Loop through the resultswhile ($items = mysql_fetch_array($result, MYSQL_ASSOC)) { // Display stuff echo "{$items['image']}<br />";}[/code]With just using the $result = mysql_query() Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29366 Share on other sites More sharing options...
kikilahooch Posted April 21, 2006 Author Share Posted April 21, 2006 I just copied and pasted that code in like this:[code]$result = mysql_query($sql, $conn) or die(mysql_error());//Loop through the resultswhile ($items = mysql_fetch_array($result, MYSQL_ASSOC)) { // Display stuff echo "{$items['image']}<br />";} if($result){ echo' <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1> <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8"> <tr> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td> </tr>'; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo'<tr> <td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td><td align="center"><b>'.$row['prodName'].'</td></b><td align="center"><b>€'.$row['price'].'</td></b>[/code]But what that did was just th displayed name of the image like i had stored in my db. And in my table nothing showed up anymore. The field that is in my db only stores the name of the image, not the actual image itself. Any other suggestions or did i put the code in wrong?? Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29370 Share on other sites More sharing options...
Orio Posted April 21, 2006 Share Posted April 21, 2006 What's this for:[code]$sql="select * from product";$sql ="select image, prodName ,price from product where dept = 'ladies'";[/code]I mean, you set $sql as something and just right away as something else?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29372 Share on other sites More sharing options...
kikilahooch Posted April 21, 2006 Author Share Posted April 21, 2006 Thats just from me trying out different stuff and not tidying up the code yet. I dont think that affects what my problem though does it?? Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29375 Share on other sites More sharing options...
Orio Posted April 21, 2006 Share Posted April 21, 2006 No it doesnt affect the code.What error do you get?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29388 Share on other sites More sharing options...
kikilahooch Posted April 21, 2006 Author Share Posted April 21, 2006 its not actually giving me back any errors. When i was using my own code it was displaying the product name and price of each product returned from the search which I wanted it to but it kept on showing me the same image in every row instead of different images. When I used sharkbaits for loop it just returned all of the image names, not displaying the actual image in the table which i wantusing this code it will display the image, but keeps on showing the same one. So i think i need to use a loop here somewhere which is what sharkbait was saying but I'm not the best at this so I'm not able to do it.[code]//<td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$image.'" ></td><td align="center"><b>'.$row['prodName'].'</td></b><td align="center"><b>€'.$row['price'].'</td></b>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29440 Share on other sites More sharing options...
eves Posted April 22, 2006 Share Posted April 22, 2006 1. on your first post, there is something wrong with the line [code]$result = @mysql_query($query);[/code] can't find value for your $query variable 2. check for authorization first before performing anymore actions to avoid wasting resources3. try this one out:[code]//check for authorization first// if not authorized, redirect or display a message or something$sql ="select image, prodName ,price from product where dept = 'ladies'";$result = mysql_query($sql,$conn) or die(mysql_error());if (mysql_num_rows($result)>1){echo' <h1><font color="#FF6600"><center> Ladies Wear</center></font></h1> <table align="center" cellspacing="0" cellpadding="5" bgcolor="#ffffff" border=1 bordercolor="#2696b8"> <tr> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Image</b></center></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Product Name</b></td> <td align="left" bgcolor="#2696b8"><center><font color="#FFFFFF"><b>Price</b></td> </tr>'; while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo'<tr> <td align="center" width="150" height="200"><img src="http://snet.wit.ie/~ciaracousins/clothes/'.$row['image'].'" ></td><td align="center"><b>'.$row['prodName'].'</td></b><td align="center"><b>€'.$row['price'].'</td></b></tr>';}echo'</table>';}[/code]didn't check for typos, you might want to double check that.Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/8051-displaying-images-dynamically-from-a-db/#findComment-29600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.