PDO prepare and execute woe

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amygdala

    PDO prepare and execute woe

    Hi,

    I am having an awful lot of trouble of getting the PDO layer to work
    properly.

    example code:
    -----------------
    $tmpArray = array (
    ':table' ='session',
    ':id' ='4364564574574 576',
    ':user_id' ='0',
    ':state' ='0',
    ':start' ='2006-08-10 00:00:00',
    ':last_update' ='2006-08-10 00:00:00',
    ':last_id_updat e' ='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(P DO::ATTR_ERRMOD E, PDO::ERRMODE_EX CEPTION);
    $sth = $dbh->prepare($sql );
    try {
    $sth->execute($tmpAr ray);
    } 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


  • Richard Levasseur

    #2
    Re: PDO prepare and execute woe


    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' ='4364564574574 576',
    ':user_id' ='0',
    ':state' ='0',
    ':start' ='2006-08-10 00:00:00',
    ':last_update' ='2006-08-10 00:00:00',
    ':last_id_updat e' ='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(P DO::ATTR_ERRMOD E, PDO::ERRMODE_EX CEPTION);
    $sth = $dbh->prepare($sql );
    try {
    $sth->execute($tmpAr ray);
    } 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.

    Comment

    • amygdala

      #3
      Re: PDO prepare and execute woe

      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.
      >
      Aha, i was afraid that that was the case :(
      Thanks!


      Comment

      Working...