imperium2335 Posted March 13, 2009 Share Posted March 13, 2009 Hi, im trying to call a variable that has a $ in it, how do I do this? the var is coming from a mysql database and is e.g. "under $5" which is assigned to $price Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/ Share on other sites More sharing options...
ram4nd Posted March 13, 2009 Share Posted March 13, 2009 use '' instead of ""; Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783537 Share on other sites More sharing options...
imperium2335 Posted March 13, 2009 Author Share Posted March 13, 2009 I have tried that, the php is comparing what the user selected from a drop down box e.g. Under £500 or $750 Then in the database i have afew test records, some have Under £500 or $750 as a varchar. But if i try to search for records with Under £500 or $750 i get a white page (doesnt work, dont know how to get php error handling to work on my localhost im using apache). Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783579 Share on other sites More sharing options...
dgoosens Posted March 13, 2009 Share Posted March 13, 2009 it might help to wrap it with single quotes... '$5' Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783582 Share on other sites More sharing options...
imperium2335 Posted March 13, 2009 Author Share Posted March 13, 2009 its not that im sure, off top of my head i remember what my code is: $pricex = "price LIKE '%$price%'"; then the query that returns $pricex As a side question, what are the %s for? Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783585 Share on other sites More sharing options...
dgoosens Posted March 13, 2009 Share Posted March 13, 2009 its not that im sure, off top of my head i remember what my code is: $pricex = "price LIKE '%$price%'"; then the query that returns $pricex I am affraid I don't really understand what you mean... the % stand for the * you would use in Google Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783590 Share on other sites More sharing options...
imperium2335 Posted March 13, 2009 Author Share Posted March 13, 2009 Thanks, I will try to post abit of my code later to see if that helps. I mean what things would be different if i did $pricex = "price LIKE '$price'"; without the %s? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783591 Share on other sites More sharing options...
dgoosens Posted March 13, 2009 Share Posted March 13, 2009 in that case, the query would only return the cells that contain exactly the value of $price. for instance if you have $price = "cheap"; if you don't put any % in the query $q = "price LIKE '$price'"; it would return only cheap if you put a % in front of it in your query $q = "price LIKE '%$price'"; it would return the cells containing cheap very cheap if you put a % behind it $q = "price LIKE '$price%'"; it would return cheap cheap a hell and if you use both $q = "price LIKE '%$price%'"; you'd get cheap very cheap very cheap indeed cheap as hell Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783596 Share on other sites More sharing options...
kickstart Posted March 13, 2009 Share Posted March 13, 2009 Hi Php is not going to do a variable substitution for a string which has had a variable concatenated into it whcih just happens to contain a $. It is did then potentially it would create an infinate loop. If you had a variable assignment that was $burt = "hello there ".$fred; while $fred contained '$bill' and you had a variable called $bill containing 100 then you could manage to get $burt to contain "hello there 100" with:- $burt = "hello there ".$fred; $burt = eval($burt); All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783604 Share on other sites More sharing options...
imperium2335 Posted March 13, 2009 Author Share Posted March 13, 2009 Hey guys thanks for your help. I will try your ideas when i get home. dgoosens you answered a question i didn't think of yet I was going to ask how to check fields that had many keywords in them and return the result if the user entered just one of them. I guess using your explanations I can do that quite easily, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783625 Share on other sites More sharing options...
imperium2335 Posted March 13, 2009 Author Share Posted March 13, 2009 Here is my code: <?PHP if (!file_exists("dbconnect.php")) { die("Database settings not found, administrator intervention required.") ; } else { require("dbconnect.php") ; //Must connect to the database. } $word = $_REQUEST['word'] ; //Variables from the form. $type = $_REQUEST['type'] ; $flavor = $_REQUEST['flavor'] ; $tiers = $_REQUEST['tiers'] ; $serves = $_REQUEST['serves'] ; $price = $_REQUEST['price'] ; $def = "Dont Specify" ; if(!isset($word) || $word == "Enter Search Term" || $word == "") { unset($word) ; } if($type == $def) { unset($type) ; } if($flavor == $def) { unset($flavor) ; } if($tiers == $def) { unset($tiers) ; } if($serves == $def) { unset($serves) ; } if($price == $def) { unset($price) ; } if(!isset($word) && !isset($type) && !isset($flavor) && !isset($tiers) && !isset($serves) && !isset($price)) { echo "You did not specify any search parameters" ; die ; } if(isset($word)) { $wordx = "tags LIKE '%$word%'" ; } if(isset($type)) { if(isset($word)) { $typex = "&& type LIKE '%$type%'" ; } else $typex = "type LIKE '%$type%'" ; } if(isset($flavor)) { if(isset($word) || isset($type)) { $flavorx = "&& flavor LIKE '%$flavor%'" ; } else $flavorx = "flavor LIKE '%$flavor%'" ; } if(isset($tiers)) { if(isset($word) || isset($type) || isset($flavor)) { $tiersx = "&& tiers LIKE '%$tiers%'" ; } else $tiersx = "tiers LIKE '%$tiers%'" ; } if(isset($serves)) { if(isset($word) || isset($type) || isset($flavor) || isset($tiers)) { $servesx = "&& serves LIKE '%$serves%'" ; } else $servesx = "serves LIKE '%$serves%'" ; } if(isset($price)) { if(isset($word) || isset($type) || isset($flavor) || isset($tiers) || isset($serves)) { $pricex = "&& price LIKE '%$price%'" ; } else $pricex = "price LIKE '%$price%'" ; } $query = "SELECT * FROM image_bank WHERE $wordx $typex $flavorx $tiersx $servesx $pricex" ; $result = mysql_query($query) ; while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $dbtags = $row['tags'] ; $dbtiers = $row['tiers'] ; $dbtype = $row['type'] ; $dbflavor = $row['flavor'] ; $dbserves = $row['serves'] ; $dbprice = $row['price'] ; $imgurl = $row['url']; $thumburl = $row['thumb_url'] ; echo "$dbtags" . "<br />" ; echo "$dbtiers" . "<br />" ; echo "$dbtype" . "<br />" ; echo "$dbflavor" . "<br />" ; echo "$dbserves" . "<br />" ; echo "$dbprice" . "<br />" ; echo '<a href="' . "$imgurl" . '">' . "$imgurl" . "</a>" . "<br />" ; echo '<img src="' . "$thumburl" . '" height="100" width="50" />' ; } include("dbdisconnect.php") ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149207-in-mysql-field/#findComment-783681 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.