Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-23-2012, 05:15 AM   PM User | #1
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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..
markman641 is offline   Reply With Quote
Old 09-24-2012, 01:07 PM   PM User | #2
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
I seem to have stumped everyone?
markman641 is offline   Reply With Quote
Old 09-24-2012, 02:23 PM   PM User | #3
alemcherry
New Coder

 
Join Date: Apr 2010
Posts: 55
Thanks: 0
Thanked 4 Times in 4 Posts
alemcherry is an unknown quantity at this point
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
alemcherry is offline   Reply With Quote
Old 09-24-2012, 02:46 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
^ 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.
Fou-Lu is offline   Reply With Quote
Old 09-24-2012, 02:53 PM   PM User | #5
Keleth
Senior Coder

 
Join Date: Jun 2008
Location: New Jersey
Posts: 2,354
Thanks: 45
Thanked 247 Times in 244 Posts
Keleth is on a distinguished road
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.
Keleth is offline   Reply With Quote
Old 10-03-2012, 12:45 AM   PM User | #6
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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];

}

?>
markman641 is offline   Reply With Quote
Old 10-03-2012, 01:35 AM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
markman641 (10-03-2012)
Old 10-03-2012, 01:40 AM   PM User | #8
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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
markman641 is offline   Reply With Quote
Old 10-03-2012, 01:44 AM   PM User | #9
markman641
Regular Coder

 
Join Date: Jul 2011
Posts: 246
Thanks: 58
Thanked 1 Time in 1 Post
markman641 has a little shameless behaviour in the past
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!
markman641 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:15 PM.


Advertisement
Log in to turn off these ads.