AndreGuatemala Posted August 29, 2008 Share Posted August 29, 2008 Does anyone know why this code does not work with the var? $getTitle = urldecode(substr($_SERVER['REQUEST_URI'], 10)); $realID = $db->get_var("SELECT id FROM ".TBL_MEMB." WHERE title='".$getTitle."'"); I'm grabbing part of the URL and using it as a var to dump into a SQL query. The var will not evaluate and the query fails. If I do an ECHO to check the $getTitle, it shows correctly. Any ideas? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/ Share on other sites More sharing options...
genericnumber1 Posted August 29, 2008 Share Posted August 29, 2008 try echoing mysql_error() after the query... it may not work depending on how your db class is constructed. If it does not work I would suggest looking into how to check the mysql error with the class of functions you are using. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-628512 Share on other sites More sharing options...
Psycho Posted August 29, 2008 Share Posted August 29, 2008 Are you even sure that TBL_MEMB is being evaluated correctly? What is the error you get??? I always create my queries as a string variable so I can echo them to the page if there is an error. $getTitle = urldecode(substr($_SERVER['REQUEST_URI'], 10)); $query = "SELECT id FROM ".TBL_MEMB." WHERE title='$getTitle'"; $realID = $db->get_var($query); You don't need to "break out" of the quotes for the $getTitle variable since you are using double quotes. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-628515 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 The thing is, there is no error. The $getTitle var is empty within the query string. If i echo it before and after the query string, it shows up but it's not being evaluated within the query: echo $getTitle; /// echo it here and the contents show up $getTitle = urldecode(substr($_SERVER['REQUEST_URI'], 10)); $query = "SELECT id FROM ".TBL_MEMB." WHERE title='$getTitle'"; /// get title here is empty $realID = $db->get_var($query); echo $getTitle; /// echo it here and the contents show up Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-628908 Share on other sites More sharing options...
BlueSkyIS Posted August 29, 2008 Share Posted August 29, 2008 have you echo'd query to make sure it's what you expect? $query = "SELECT id FROM ".TBL_MEMB." WHERE title='$getTitle'"; /// get title here is empty echo "query: $query<BR>"; Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-628987 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 have you echo'd query to make sure it's what you expect? $query = "SELECT id FROM ".TBL_MEMB." WHERE title='$getTitle'"; /// get title here is empty echo "query: $query<BR>"; Yes and the echo shows the correct query but the next step doesn't seem to work: $realID = $db->get_var($query); the query does not bring back and results, however if I switch out the $getTitle with actually text, the query works fine. For some reason it doesn't like the $ in the $query. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629008 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Are you sure there's any results for it to give back? Try running the outputted query in phpMyAdmin or the MySQL client. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629012 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 Are you sure there's any results for it to give back? Try running the outputted query in phpMyAdmin or the MySQL client. Yes I did that and it returns results. Even when I put literal text into the query it works fine. Only when I use a $ does it stop working. I'm stumped?? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629017 Share on other sites More sharing options...
genericnumber1 Posted August 29, 2008 Share Posted August 29, 2008 We'd have to see the class with the get_var() method. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629084 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 We'd have to see the class with the get_var() method. here it is: function get_var($sql){ $res = $this->query($sql, 'Var select failed'); list($var) = mysql_fetch_row($res); mysql_free_result($res); return $var; } Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629103 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Can we see the query() method of that same class? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629110 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 Can we see the query() method of that same class? function query($sql, $err_text='QUERY failed', $err_type='Fatal'){ if(DEBUG_LEVEL & (4 | ) { $this->QUERY_LOG[] = $sql; $tmp = cpGMT(); $res = mysql_query($sql, $this->_DB_CONN); $this->QUERY_TIME[] = -$tmp + cpGMT(); } else $res = mysql_query($sql, $this->_DB_CONN); if(!$res) $this->_error($err_text, $err_type, $sql); else return $res; } Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629116 Share on other sites More sharing options...
BlueSkyIS Posted August 29, 2008 Share Posted August 29, 2008 i don't know what $getTitle looks like, but you might want to try wrapping it in mysql_real_escape_string before using it in SQL: $getTitle = mysql_real_escape_string($getTitle); Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629121 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Already suggested that to him over PM, and he said it didn't work. >_> This is weird. Try just using the normal mysql_* functions instead of that wrapper class? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629122 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 Already suggested that to him over PM, and he said it didn't work. >_> This is weird. Try just using the normal mysql_* functions instead of that wrapper class? Crazy huh? Yup I tried just a straight mysql call (bypassing the class) and it's the same problem. For some reason it doesn't like the $ in the query call. Again, if I express it literally (without changing anything else), it works fine. For example: (this does NOT work) $getTitle = substr((urldecode($_SERVER['REQUEST_URI'])), 10); $query = "SELECT id FROM ".TBL_MEMB." WHERE title = '".$getTitle."'"; $realID = $db->get_var($query); (this DOES work) $getTitle = 'name goes here'; $query = "SELECT id FROM ".TBL_MEMB." WHERE title = '".$getTitle."'"; $realID = $db->get_var($query); I don't know?? Is it some kind of conversion issue? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629139 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 So wait, can I see what you did with the normal MySQL calls? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629140 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 So wait, can I see what you did with the normal MySQL calls? Here's the mysql calls. I cut out all the obvious connection setup stuff $setupQ = mysql_query($query); $Qinfo = mysql_fetch_row($setupQ); echo $Qinfo[0]; The $query is the same from above. Again, it works when you put in a literal string. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629158 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 I really want to say that it has to do with the urldecode() and the substr() calls. What are you trying to get out of the $_SERVER['REQUEST_URI'] variable? Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629164 Share on other sites More sharing options...
AndreGuatemala Posted August 29, 2008 Author Share Posted August 29, 2008 I really want to say that it has to do with the urldecode() and the substr() calls. What are you trying to get out of the $_SERVER['REQUEST_URI'] variable? I'm thinking that is the problem too, but when I ECHO the $, it shows correctly but the query just doesn't like it. I am basically trying to grab part of the URL and use it for my query. I'm trying to keep the url SEO friendly. For example. www.blah.com/title of thing here I want to grab the "title of thing here" (which is the same as the title field in my db) and use it in my query. I'm using this: substr((urldecode($_SERVER['REQUEST_URI'])), 10) it grab the url and trim all the stuff before the "/". this leaves me with the part I want. Then I want to slap that in my $ and use it in the query. Link to comment https://forums.phpfreaks.com/topic/121819-string-is-not-evaluating-correctly-anyone-help/#findComment-629174 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.