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? Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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>"; Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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?? Quote Link to comment 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. Quote Link to comment 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; } Quote Link to comment 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? Quote Link to comment 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; } Quote Link to comment 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); Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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.