Jump to content

String is not evaluating correctly. Anyone help?


AndreGuatemala

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

Can we see the query() method of that same class?

 

 

  function query($sql, $err_text='QUERY failed', $err_type='Fatal'){

    if(DEBUG_LEVEL & (4 | 8)) {

      $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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.