You are here: Re: PDO prepare and execute woe « PHP Programming Language « IT news, forums, messages
Re: PDO prepare and execute woe

Posted by Richard Levasseur on 08/11/06 10:14

amygdala wrote:
> Hi,
>
> I am having an awful lot of trouble of getting the PDO layer to work
> properly.
>
> example code:
> -----------------
> $tmpArray = array (
> ':table' => 'session',
> ':id' => '4364564574574576',
> ':user_id' => '0',
> ':state' => '0',
> ':start' => '2006-08-10 00:00:00',
> ':last_update' => '2006-08-10 00:00:00',
> ':last_id_update' => '2006-08-10 00:00:00',
> ':ip' => '123456789',
> ':agent' => 'Opera',
> ':referer' => 'whatever.php',
> ':data' => 'whatever data');
>
> $sql = 'INSERT INTO :table (id, user_id, state, start, last_update,
> last_id_update, ip, agent, referer, data) VALUES (
> :id,
> :user_id,
> :state,
> :start,
> :last_update,
> :last_id_update,
> :ip,
> :agent,
> :referer,
> :data
> )';
>
> $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
> $sth = $dbh->prepare($sql);
> try {
> $sth->execute($tmpArray);
> } catch (PDOException $e) {
> die('Error!: ' . $e->getMessage());
> }
>
> returns:
>
> Error!: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
> error in your SQL syntax; check the manual that corresponds to your MySQL
> server version for the right syntax to use near ''session' (id, user_id,
> state, start, last_update, last_id_update, ip, agent, re' at line 1
>
> What gives?
>
> If I hardcode the table in the SQL statement it seems to work fine. But this
> should work too shouldn't it?
>
> Cheers

It's a bit frustrating, but you can't use the place holders in all
spots. The exact places you can use it, I don't know. It probably
depends on if the prepare() is emulated or not, and what the underlying
server supports.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация