Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0

    Pdo fetch is not returning anything


    Here is the code. This does not echo anything.

    PHP Code:
    $sql "SELECT value1 FROM {$table} WHERE value=\":value\"";

    $st $connection->prepare($sql);
    $st->bindValue(":value"$valuePDO::PARAM_STR);

    $result $st->fetch(PDO::FETCH_ASSOC);
    $value1 $result[value1];

    echo 
    $value1
    When i try to echo the $value1 variable it does not do anything but when i do this same query in PHPMyAdmin it works. So it has to do with the fetch code. Any help is appreciated.
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,141
    Rep Power
    9398
    Originally Posted by jblevins1991
    So it has to do with the fetch code.
    It sure does. Take a look at the example code for fetch(), then take a look at how your code differs from it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    That is irrelevant. I am not using mysqli. I am using PDO. I would post the url for php.net's pdo library but i cannot. you should look it up.

    Comments on this post

    • requinix agrees : you're absolutely right
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    Here is the code. This does not echo anything.

    PHP Code:
    $value1 $result[value1]; 
    When i try to echo the $value1 variable it does not do anything but when i do this same query in PHPMyAdmin it works. So it has to do with the fetch code. Any help is appreciated.
    I would have thought with the returned array being indexed by column name, that the index should be quoted, like this.

    Code:
    $value1 = $result['value1'];
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Still nothing sadly :\.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    Still nothing sadly :\.
    What if you turn on the general log and take a look at what query is actually being built/sent to mysql?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    how do i do that. i just assumed it would work seeing as it did in myphpadmin.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    how do i do that. i just assumed it would work seeing as it did in myphpadmin.
    Is it your server? What version of mysql?

    In V 5.1.something, you can turn logging on/off without restarting the server.
    Code:
    SET GLOBAL log_output = 'TABLE';
    SET GLOBAL general_log=1;
    Make your query call to the server.
    Turn off logging.
    Code:
    SET GLOBAL general_log=0;
    Retrieve your data from the mysql.general_log table
    Code:
    SELECT * FROM mysql.general_log;
    If it's not your server then I expect you won't be able to change global variables. You should set up your own test environment on a machine you control to develop/debug you application, then deploy to production once it all works.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by BarryG
    Is it your server? What version of mysql?

    In V 5.1.something, you can turn logging on/off without restarting the server.
    Code:
    SET GLOBAL log_output = 'TABLE';
    SET GLOBAL general_log=1;
    Make your query call to the server.
    Turn off logging.
    Code:
    SET GLOBAL general_log=0;
    Retrieve your data from the mysql.general_log table
    Code:
    SELECT * FROM mysql.general_log;
    If it's not your server then I expect you won't be able to change global variables. You should set up your own test environment on a machine you control to develop/debug you application, then deploy to production once it all works.
    The query is going through. I did this to test it.

    PHP Code:
     $sql "SELECT value1 FROM {$table} WHERE value=\":value\""

    $st $connection->prepare($sql); 
    $st->bindValue(":value"$valuePDO::PARAM_STR); 
    $m $st->execute();

    if(
    $m == true)
    {
    echo
    "it works";
    }
    else
    {
    echo
    "no dice";
    }


    $result $st->fetch(PDO::FETCH_ASSOC); 
    $value1 $result[value1]; 

    echo 
    $value1
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    The query is going through. I did this to test it.
    But exactly WHAT query is going through? And what does it return?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by BarryG
    But exactly WHAT query is going through? And what does it return?
    The pdo::execute method returns true or false and it executes the query that is prepared by the prepare method. The same query stored in the $sql variable.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    The pdo::execute method returns true or false and it executes the query that is prepared by the prepare method. The same query stored in the $sql variable.
    What I'm trying to get you to determine is exactly what text is being assembled in the $sql variable. It might not be exactly what you think it is. When you manually enter the command in the PhpMyAdmin sql tool, you get the expected result. In PDO, you don't. But PDO is assembling the sql from a bunch of parts. It can be very useful to see the final sql that has been assembled.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by BarryG
    What I'm trying to get you to determine is exactly what text is being assembled in the $sql variable. It might not be exactly what you think it is. When you manually enter the command in the PhpMyAdmin sql tool, you get the expected result. In PDO, you don't. But PDO is assembling the sql from a bunch of parts. It can be very useful to see the final sql that has been assembled.
    I see what you mean. I have an account with hostgator. Where do i place those commands?
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    Sydney Australia
    Posts
    186
    Rep Power
    84
    Originally Posted by jblevins1991
    I see what you mean. I have an account with hostgator. Where do i place those commands?
    Like I said a few posts ago.

    You should set up your own test environment on a machine you control to develop/debug you application, then deploy to production once it all works.

    One other thing I'm not sure about is the line
    PHP Code:
    $sql "SELECT value1 FROM {$table} WHERE value=\":value\""
    What if you try leaving out the { and } like this.
    PHP Code:
    $sql "SELECT value1 FROM $table WHERE value=\":value\""
    If that's no good, then the old reliable is
    PHP Code:
    $sql "SELECT value1 FROM " $table " WHERE value=\":value\""
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by BarryG
    Like I said a few posts ago.

    You should set up your own test environment on a machine you control to develop/debug you application, then deploy to production once it all works.

    One other thing I'm not sure about is the line
    PHP Code:
    $sql "SELECT value1 FROM {$table} WHERE value=\":value\""
    What if you try leaving out the { and } like this.
    PHP Code:
    $sql "SELECT value1 FROM $table WHERE value=\":value\""
    If that's no good, then the old reliable is
    PHP Code:
    $sql "SELECT value1 FROM " $table " WHERE value=\":value\""
    this did not work either. I went back to using the brackets.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo