SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple straight up query problem

    I have a simple query shown here:

    $result = $conn->query("select user from activation where token = '$token' ");

    There is no result; however, I am sure that the connection is good, that $token has a value that is in the table, and that the spelling for the table and fields are correct.

    If I substitute the actual value of $token (which I var_dumped to confirm) like this:

    $result = $conn->query("select user from activation where token = 'f0ef1bc00c1628b7ae94f6d881b55f59' ");

    I get the results I want.

    Any ideas why I have this silly problem?

  2. #2
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What database abstraction library are you using? Could this be a problem with how it parses the string containing the query? What happens if you use a place holder instead of placing the variable directly in the string?

    PHP Code:
    $result $conn->query('select user from activation where token=?', array($token)); 

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would I find out which database abstraction query I'm using? I tried your code and I got this warning: "Warning: query() expects parameter 2 to be long, array given"

    I'm pretty much at a loss here because everything works in isolation- the connection, the table, the variable. I've never had this problem before. Could this problem be due to the unusual length of the string? Does PHP autotrim variables when interacting with mysql?

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I apologize for making assumptions and not communicating very well here.

    My question was whether you are using PDO to perform this query or if you are making use of a third party library such as PEAR::MDB2 or ADOdb. Judging by the error message I am guessing (making assumptions yet again!) that you are using PDO?

    You might want to play around with the query to make sure you are generating the same string. For example:

    PHP Code:
    // debugging
    $token 'f0ef1bc00c1628b7ae94f6d881b55f59';
    $sql1 "select user from activation where token = '$token'";
    $sql2 'select user from activation where token = \'' $token '\'';

    echo 
    "select user from activation where token = 'f0ef1bc00c1628b7ae94f6d881b55f59'";
    echo 
    "<br />\n";
    echo 
    $sql1;
    echo 
    "<br />\n";
    echo 
    $sql2
    If the three lines are not exactly the same you can compare them to see where the difference creeps in.

    Alternately you can take a look at the 'prepare' and 'execute' syntax as an example of how to use "bind parameters". http://www.php.net/manual/en/function.PDO-prepare.php

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2005
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sucess!

    Apparently I absentmidedly added a comma in the activation link so that when I wrote $token = $_GET['userid']; the userid had a comma on the end.

    Thanks for the help Trent, that silly little mistake was driving me nuts.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •