Recon Posted December 19, 2006 Share Posted December 19, 2006 Here is my code, I'm reading from a MySQL db.[code]$username="*******"; $password="*******"; $database="***********"; mysql_connect(localhost,$username,$password); @mysql_select_db($database) or die("Unable to select database."); //get products $queryproducts="SELECT * FROM products WHERE status='available' ORDER BY price"; $resultproducts=mysql_query($queryproducts); $numproducts=mysql_numrows($resultproducts); mysql_close(); $iproduct=0; while ($iproduct < $numproducts) { $id=mysql_result($resultproducts,$iproduct,"id"); $name=mysql_result($resultproducts,$iproduct,"name"); $price=mysql_result($resultproducts,$iproduct,"price"); $img2 = 'img/products/$id/2.jpg'; $img3 = 'img/products/$id/3.jpg'; $img4 = 'img/products/$id/4.jpg'; echo "<img src='phpThumb.php?src=img/products/$id/1.jpg&w=150px&h=150px&zc=1' alt='$name' width='150' height='150' /> <h3>$name</h3> <p>Price: £$price</p> <h4>BUY NOW</h4>"; if (file_exists($img2)) { echo "<img src='phpThumb.php?src=img/products/$id/2.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";} else { echo "sorry no2";} if (file_exists($img3)) { echo "<img src='phpThumb.php?src=img/products/$id/3.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";} else { echo "sorry no3"; } if (file_exists($img4)) { echo "<img src='phpThumb.php?src=img/products/$id/4.jpg&w=100px&h=40px&zc=1' width='100' height='40' alt='Image'>";} else { echo "sorry no4";}$iproduct++;}?>[/code]It always shows the "sorry" text even though I definitely have the files there. If change the:[code]$img2 = 'img/products/$id/2.jpg'; $img3 = 'img/products/$id/3.jpg'; $img4 = 'img/products/$id/4.jpg';[/code]to[code]$img2 = 'img/products/1/2.jpg'; $img3 = 'img/products/1/3.jpg'; $img4 = 'img/products/1/4.jpg';[/code]it works so it must be a problem with 'inserting' (sorry I'm a newbie) the $id there.Edit: I tried replacing $id with " . $id . " (advice from a friend) but it still doesn't work. Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/ Share on other sites More sharing options...
artacus Posted December 19, 2006 Share Posted December 19, 2006 How is this a MySQL problem? This is definately a PHP problem. Actually its a single quotes vs double quotes issue. Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/#findComment-144787 Share on other sites More sharing options...
fenway Posted December 19, 2006 Share Posted December 19, 2006 You need to use double-string literals if you want your variables to be interpolated -- or do it properly, concatenating and all. Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/#findComment-144801 Share on other sites More sharing options...
artacus Posted December 19, 2006 Share Posted December 19, 2006 *puts $5 down that Recon will ask you to spell out what you just said* Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/#findComment-144802 Share on other sites More sharing options...
Recon Posted December 20, 2006 Author Share Posted December 20, 2006 [quote author=artacus link=topic=119297.msg488534#msg488534 date=1166568983]*puts $5 down that Recon will ask you to spell out what you just said*[/quote]lol, I would have done but a friend helped me out. I think I ended up doing what you said anyway...[code] $img1 = "img/products/".$id."/1.jpg"; $img2 = "img/products/".$id."/2.jpg"; $img3 = "img/products/".$id."/3.jpg"; $img4 = "img/products/".$id."/4.jpg";[/code]worked in the end. :) Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/#findComment-144855 Share on other sites More sharing options...
artacus Posted December 20, 2006 Share Posted December 20, 2006 hehe, yep, that's what we were talking about. Link to comment https://forums.phpfreaks.com/topic/31281-solved-problem-with-if-and-file_exists/#findComment-144877 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.