沃梦达 / 编程问答 / php问题 / 正文

Yii Framework/PDO 获取错误 CDbCommand 无法执行 SQL 语句:SQLSTATE[42000]

Yii Framework/PDO getting error CDbCommand failed to execute the SQL statement: SQLSTATE[42000](Yii Framework/PDO 获取错误 CDbCommand 无法执行 SQL 语句:SQLSTATE[42000])

本文介绍了Yii Framework/PDO 获取错误 CDbCommand 无法执行 SQL 语句:SQLSTATE[42000]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Yii 框架和 PDO 对象将一些数据插入表中,但出现以下错误

I'm trying to insert some data into a table using the Yii Framework together with the PDO object and get the following error

我正在使用此代码构建查询

I'm building the query using this code

$connection = CActiveRecord::getDbConnection();

        $sql="INSERT INTO sms_logs (to, from, message,error_code,date_send) VALUES (:to,:from,:message,:error_code,:date_send)";
        $command=$connection->createCommand($sql);
        $command->bindParam(":to",$to,PDO::PARAM_STR);
        $command->bindParam(":from",$from,PDO::PARAM_STR);
        $command->bindParam(":message",$message,PDO::PARAM_STR);
        $command->bindParam(":error_code",$code,PDO::PARAM_STR);
        $command->bindParam(":date_send",date("Y-m-d H:i:s"),PDO::PARAM_STR);
        $command->execute();

然后只要我运行代码,我就会得到

And then as soon as I run the code I get

CDbCommand failed to execute the SQL statement: 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 'to, from, message,error_code,date_send) VALUES ('27724963345','27723663542','Hap' at line 1INSERT INTO sms_logs (to, from, message,error_code,date_send) VALUES (:to,:from,:message,:error_code,:date_send)

欢迎提出任何建议!使用 mySql 作为数据库

any suggestions will be welcome! using mySql as the db

推荐答案

您需要对 $sql 中的 from 一词进行转义.这是一个保留字.

You need to escape the word from in your $sql. It is a reserved word.

这篇关于Yii Framework/PDO 获取错误 CDbCommand 无法执行 SQL 语句:SQLSTATE[42000]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Yii Framework/PDO 获取错误 CDbCommand 无法执行 SQL 语句:SQLSTATE[42000]

基础教程推荐