Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post

    Using a value to select table in PDO/DBH

    Hi, I'm trying to use a PHP variable to select my table ($currentFile gets the folder the page is in) except i keep getting this error:

    Fatal error: Call to a member function execute() on a non-object in /home/quizbona/public_html/campaigns/1/index.php on line 18
    And yes, I'm positive it's the value causing it to mess up. it worked until I put that in there.

    Neither of these work:

    PHP Code:
    <?
    //the SQL we want to execute
    $sql 'SELECT * FROM ?';
    //prepare the query
    $sth $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    //execute the query
    $sth->execute(array($currentFile));
    //fetch all rows
    $words $sth->fetchAll();
    //print all rows
    foreach($words as $word) {
    ?>
    ........
    PHP Code:
    <?
    //the SQL we want to execute
    $sql 'SELECT * FROM $currentFile';
    //prepare the query
    $sth $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    //execute the query
    $sth->execute(array());
    //fetch all rows
    $words $sth->fetchAll();
    //print all rows
    foreach($words as $word) {
    ?>
    .....
    Last edited by markman641; 09-23-2012 at 06:20 AM.

  • #2
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    I seem to have stumped everyone?

  • #3
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    There is an error creating the object dear, you need to have proper error handling.

    Code:
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY))
          or die "Can't prepare SQL statement: $DBI::errstr\n";
    will give you the reason.
    Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    ^ this is correct for your actual error.
    However, I don't believe you can bound any structure as a part of the statement. The table name would include the structure; statements are designed to separate the structure of the table with the data to go within the table, so you can bind when providing information to insert or query from for example, but not for table names or properties.

    Your second one doesn't work as the table name probably isn't $currentFile. Variables are not parsed in single quotations.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Yup, I can confirm that though it would be nice, you cannot prepare a table name. As PDO internally quotes or converts to a number or whatever it may be, when you try to add a table, it seems to treat it as a literal string, and a table name is not a literal string.

    You'll either have to sanitize yourself and add it to the actual string before the prepare, or type it in. Given you should always know what the possible table names are, you can easily create an if series to test entered names against known names and only continue if there is a match.

  • #6
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Okay, for some reason this code isn't even working at all. It outputs nothing..

    PHP Code:
    <?php

    //create the database object
    $dbh = new PDO('mysql:host=localhost;dbname=xxxxxx''xxxx''xxxxx');

    //the SQL we want to execute
    $sql 'SELECT * FROM 1 WHERE :all';

    //prepare the query
    $sth $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY))
          or die (
    "Can't prepare SQL statement: $DBI::errstr\n");

    //execute the query
    $sth->execute(array(":all" => "1"));

    //fetch all rows
    $words $sth->fetchAll();

    //print all rows
    foreach($words as $word) {
      echo 
    $word[0];

    }

    ?>

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Works for me; I changed only the table name to something other than 1. That would indicate to me that you have no records in your table 1.

  • Users who have thanked Fou-Lu for this post:

    markman641 (10-03-2012)

  • #8
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    Works for me; I changed only the table name to something other than 1. That would indicate to me that you have no records in your table 1.
    weird.. I do.. let me try changing the table name

  • #9
    Regular Coder
    Join Date
    Jul 2011
    Posts
    272
    Thanks
    63
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    Works for me; I changed only the table name to something other than 1. That would indicate to me that you have no records in your table 1.
    well.. would you look at that. my table can't be called 1 for some reason 0.o
    it works. thanks!


  •  

    Posting Permissions

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