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?

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.

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

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.

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??

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;

  }

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;

  }

 

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?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.