Enjoy an ad free experience by logging in. Not a member yet?
Register .
09-23-2012, 05:15 AM
PM User |
#1
Regular Coder
Join Date: Jul 2011
Posts: 246
Thanks: 58
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:
Quote:
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 05:20 AM ..
09-24-2012, 01:07 PM
PM User |
#2
Regular Coder
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
I seem to have stumped everyone?
09-24-2012, 02:23 PM
PM User |
#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.
09-24-2012, 02:46 PM
PM User |
#4
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 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.
09-24-2012, 02:53 PM
PM User |
#5
Senior Coder
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 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.
10-03-2012, 12:45 AM
PM User |
#6
Regular Coder
Join Date: Jul 2011
Posts: 246
Thanks: 58
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 ]; } ?>
10-03-2012, 01:35 AM
PM User |
#7
God Emperor
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 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:
10-03-2012, 01:40 AM
PM User |
#8
Regular Coder
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
Quote:
Originally Posted by
Fou-Lu
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
10-03-2012, 01:44 AM
PM User |
#9
Regular Coder
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
Quote:
Originally Posted by
Fou-Lu
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!
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 01:15 PM .
Advertisement
Log in to turn off these ads.