2

i did not make these column name as they are created by uploading CSV file as 'COL 1' 'COL 2'

   connect();
   global $db;
   $sql = $db->prepare("UPDATE table1 SET 'COL 1'=:c1 where table1_id=:id");
    $sql->bindParam(':id',$arry['id'],PDO::PARAM_STR, 12);

    $sql->bindParam(':c1',$arry['t1'],PDO::PARAM_STR, 12);
    try
    {
    $sql->execute();
    }
    catch(PDOException $e)
    {
    echo($e);
    }

the error showing is

exception 'PDOException' with message '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 '1='DDEE' where table1_id='5'' at line 1' in C:\wamp\www\excelsheets\functions.php:109 Stack trace: #0 C:\wamp\www\excelsheets\functions.php(109): PDOStatement->execute() #1 C:\wamp\www\excelsheets\save4.php(4): save4_update(Array) #2 {main}

I know we can't use '' single quotes in prepared statement then how can i represent a column with space in between?

10
  • 1
    backtics (below the tilde) around your column names should do it.
    – crthompson
    Apr 22, 2014 at 21:32
  • you mean 'COL 1' this is not allowed Apr 22, 2014 at 21:34
  • 1
    What about str_replace(' ', '_', $column_name) before doing the insert into the DB?
    – Mike
    Apr 22, 2014 at 21:36
  • 1
    @paqogomez I agree it's different and doesn't answer the specific problem. However you did answer it in your first comment. No sense in re-stating it again when you're already right.
    – Mike
    Apr 22, 2014 at 21:42
  • 1
    Thanks a alot `` worked in pdo Apr 22, 2014 at 21:44

1 Answer 1

3

The escape character in MySQL is the back quote (or back tick). Here is an example of what it looks like:

UPDATE table1 
    SET `COL 1` = :c1
     where table1_id = :id

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.