magento2 在代码直接运行sql,有特殊字符怎么

1.53K 浏览M2交流区magento2

magento2 在代码直接运行sql,有特殊字符怎么

我有一段代码,更新产品的meta title属性,但是它的值包含特殊符号。该怎么处理、

$table =catalog_product_entity_varchar;
$id=9718;
echo  $sql = "UPDATE ".$table." SET `value` = '".$newMetaTitleValue."' WHERE  value_id = ".$id;
$connection->query($sql);

运行上面代码出现。

 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 's Sky-ukeydeal' WHERE value_id = 9718' at line 1, query was: UPDATE catalog_product_entity_varchar SET `value` = 'No Man's Sky-ukeydeal' WHERE value_id = 9718 Exception #1 (PDOException):

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 's Sky-ukeydeal' WHERE value_id = 9718' at line 1 Exception #0 (Zend_Db_Statement_Exception):

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 's Sky-ukeydeal' WHERE value_id = 9718' at line 1, query was: UPDATE catalog_product_entity_varchar SET `value` = 'No Man's Sky-ukeydeal' WHERE value_id = 9718

哪一位老师,知道怎么解决吗

更改状态以发布
0

$newMetaTitleValue 加个addslashes($newMetaTitleValue) 转义一下

已回答
0