php - mysql_fetch_assoc() 的 PDO 方法?

标签 php mysql database pdo

我曾经做过:

$resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
$user = mysql_fetch_assoc($resource);
echo "Hello User, your number is" . $user['number'];

我读到 mysql 语句都已弃用,不应使用。

我如何使用 PDO 执行此操作?

第一行应该是:

$stmt = $db->prepare("SELECT * FROM table WHERE id = " . $id); // there was an aditional double quote in here.
$stmt->execute(array(':id' => $id));

mysql_fetch_assoc() 函数怎么样?

我正在使用 php

最佳答案

你可以使用(PDO::FETCH_ASSOC)常量
用法将是 while ($res = $stmt->fetch(PDO::FETCH_ASSOC)){ .... }
这是引用(准确的文档):http://www.php.net/manual/en/pdostatement.fetch.php

关于php - mysql_fetch_assoc() 的 PDO 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16363794/

相关文章:

php - 我的对象包含自身的多个实例,我应该担心吗?

PHP 表单返回 Notice : Array to string conversion

php - 图片 MIME 类型和扩展名正确,但上传失败

mysql - 从日期字段中过滤每个月的sql数据

php - jQuery 检查文件

mysql - 从 mysql 列中删除枚举值

mysql - 如何修复 SQL 错误 (1248) : Every derived table must have its own alias

php - 如何导出到excel?

sql - 计算唯一行

java - 数据库中的一列有多个值