我试图将输入参数绑定(bind)到我的 SELECT 查询中,然后获取结果行,但 MySQLi 似乎与我习惯的其他 API 不同,我在 PHP 手册中迷失了方向。
以下方法正确吗?
$sql = 'SELECT product_id, product_name, area_id
FROM product
WHERE product_id = ?';
$stmt = $myMySQLi->prepare($sql);
if(!$stmt){
throw new Exception('Prepare error');
}
if( !@$stmt->bind_param('i', $product_id) ){
throw new Exception('Bind error');
}
if( !$stmt->execute() ){
throw new Exception('Execute error');
}
如果是这样,我如何将行提取到关联数组中?如果我把事情搞得太复杂了,我该怎么办?
最佳答案
mysqli 不提供将结果提取到数组中的方法。如果您想要此功能,您有两个选择:
- 扩展 mysqli 并编写 fetchAll 方法
- 从现在开始使用 pdo
提示:使用pdo
只是想让你的生活更轻松。
关于php - 使用绑定(bind)变量的 SELECT 查询示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2739182/