我正在经历来自 Yii 的奇怪行为。由于某种原因,此代码无法按预期运行:
$sql = "SELECT id, class_id, lang FROM tbl_top_class WHERE class_name = :string";
$cmd = Yii::app()->db->createCommand($sql);
$data[':string'] = "sad";
$result = $cmd->queryAll($data);
$db = new PDO(...);
$stmt = $db->prepare($sql);
$stmt->execute($data);
$result2 = $stmt->fetchAll();
print_r($result);
echo "<br>";
print_r($result2);
我希望它$result
是$result2
相同的,但它们不是。这是脚本输出的内容:
Array ( [0] => Array ( [id] => 1 [class_id] => 0 [lang] => en-us ) )
Array ( )
这是数据库结构和数据:
--
-- Table structure for table `tbl_top_class`
--
CREATE TABLE `tbl_top_class` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`lang` varchar(5) NOT NULL,
`class_name` varchar(100) NOT NULL,
`class_id` bigint(20) unsigned NOT NULL,
`description` text,
PRIMARY KEY (`id`),
KEY `lang` (`lang`),
KEY `class_name` (`class_name`),
KEY `class_id` (`class_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Dumping data for table `tbl_top_class`
--
INSERT INTO `tbl_top_class` (`id`, `lang`, `class_name`, `class_id`, `description`) VALUES
(1, 'en-us', 'discard', 0, 'Non-classifiable');
两个连接都指向完全相同的数据库和表,具有完全相同的凭据。为什么我得到不同的结果?