Sunday 22 December 2019

php - how to write column name with space in pdo prepared statement



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?


Answer



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


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...