//搜索 $connection = Yii::$app->db; $user_id = Yii::$app->user->id; //print_r($user_id); /* * 报名的赛事 */ $sql = "select ybm from bm_userybm where user_id=$user_id";
$command = $connection->createCommand($sql);
$result = $command->queryAll(); /* * 获奖的赛事 */ $sql_yhj = "select yhj from bm_useryhj where user_id=$user_id";
$command_yhj = $connection->createCommand($sql_yhj);
$result_yhj = $command_yhj->queryAll(); //print_r($result); //echo "<br/>";
//添加
$sql = ” INSERT INTO `tbl_user` (`username`, `password`, `email`) VALUES (‘test’, ‘test’, ‘test@test.com’) “;
$command=$connection->createCommand($sql);
print_r($command->execute());
//添加 返回自增id
$command1 = $connection->createCommand(“SELECT last_insert_id()”);
$result = $command1->queryAll();
//常用函数
(1)如果你执行的SQL语句有返回结果集: 例如SELECT。通常用query开头的系列函数:
$dataReader=$command->query(); // 执行一个 SQL 查询
$rows=$command->queryAll(); // 查询并返回结果中的所有行
$row=$command->queryRow(); // 查询并返回结果中的第一行
$column=$command->queryColumn(); // 查询并返回结果中的第一列
$value=$command->queryScalar(); // 查询并返回结果中第一行的第一个字
(2)你执行的SQL语句返回的不是结果集,只是状态值,例如:INSERT ,UPDATE,DELETE.则用execute()
$this->command->execute();
//使用事务的一种常见情形:CDbTransaction
$transaction = $connection->beginTransaction();
try{
$connection->createCommand($sql1)->execute();
$connection->createCommand($sql2)->execute();
………
$transaction->commit();
}
catch(Exception $e){ // 如果有一条查询失败,则会抛出异常
$transaction->rollBack();
}
Yii 直接执行SQL语句的几种使用方法
1. 创建CDbCommand命令对象 $sql = "select * from users where id=1"; $connection=Yii::app()->db; $command=$connection->createCommand($sql); 2. SQL语句的执行 CDbCommand对象有两个(种)方法来执行SQL语句。 2.1 execute()方法
用于数据更新(非查询)操作(INSERT, UPDATE 和 DELETE),返回的是操作受影响的记录行数 $rowCount=$command->execute(); 2.2 query()和queryXXX()方法
用于查询,对应于SELECT语句 2.2.1 query()方法
$dataReader=$command->query(); 返回CDbDataReader对象,注意这代表结果集而不是记录,可以通过以下方法来获取(遍历)记录: // CDbDataReader::read()可以一次获取一行数据,到末尾时返回false while(($row=$dataReader->read())!==false) // CDbDataReader实现了迭代器接口因此可以使用foreach遍历 foreach($dataReader as $row) // 一次性返回所有的记录(数组) $rows=$dataReader->readAll(); 2.2.2 衍生的queryXXX()方法
返回所有结果记录数组 $rows=$command->queryAll();
返回第一行记录 $row=$command->queryRow(); // query and return the first row of result
返回所有记录的第一列 $column=$command->queryColumn(); // query and return the first column of result
返回第一行记录的第一列 $value=$command->queryScalar(); // query and return the first field in the first row
G
M
T
Detect languageAfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu | AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBelarusianBengaliBosnianBulgarianCatalanCebuanoChichewaChinese (Simplified)Chinese (Traditional)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHausaHebrewHindiHmongHungarianIcelandicIgboIndonesianIrishItalianJapaneseJavaneseKannadaKazakhKhmerKoreanLaoLatinLatvianLithuanianMacedonianMalagasyMalayMalayalamMalteseMaoriMarathiMongolianMyanmar (Burmese)NepaliNorwegianPersianPolishPortuguesePunjabiRomanianRussianSerbianSesothoSinhalaSlovakSlovenianSomaliSpanishSundaneseSwahiliSwedishTajikTamilTeluguThaiTurkishUkrainianUrduUzbekVietnameseWelshYiddishYorubaZulu |
---|
Text-to-speech function is limited to 200 characters
Options : History : Feedback : Donate | Close |
---|