本文介绍了在yii中执行sql后如何获取成功信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我需要知道如何在成功执行 sql 或错误执行失败消息后获取成功消息.我的示例如下


i need to know how to get success message after successful execution of sql or failure message of wrong execution.my example is below

          `
     public function actionSql()
      {          
            $table_no='1';
            $employee='1';
            $status='1';
    $connection=Yii::app()->db;
       $sql="INSERT INTO orders_transaction (table_no,employee,status) VALUES(:table_no,:employee,:status)";

     $command=$connection->createCommand($sql);
     $command->bindParam(":table_no",$table_no,PDO::PARAM_STR);
     $command->bindParam(":employee",$employee,PDO::PARAM_STR);
        $command->bindParam(":status",$status,PDO::PARAM_STR);

    $command->execute();} `

执行后我需要知道该行是否成功插入.
我使用了下面的一个但没有使用它唯一的回显成功而不是失败

after executing i need to know is the row successfully inserted or not.
i used below one but no use its only echoing successfully not failure

if($command->execute())
   {
    echo "Successful";
   }

   else {
      echo "ERROR";
        }

所以我尝试了这个,它给出了使用密码的本地主机的权限被拒绝错误

so i tried this one its giving permission denied error for localhost with password" "

          $result=mysql_query($sql);

    // if successfully insert data into database, displays message "Successful".
         if($result){
              echo "Successful";

                   }

            else {
               echo "ERROR";
                  }

希望你能理解我的问题.请给出任何建议或答案.

i hope you under stand my problem.please give any suggestion or answer.

推荐答案

Execute()

返回受影响的行数(对于 INSERT、DELETE、UPDATE 等).

returns the number of affected rows(for INSERT, DELETE, UPDATE etc).

$num = $command->execute();

此处 $num 将包含受影响的行数.

here $num will contain the affected number of rows.

这篇关于在yii中执行sql后如何获取成功信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 05:22