1 回答
TA贡献1810条经验 获得超4个赞
我建议使用 PDO:https ://www.php.net/manual/en/class.pdo.php 你想从 __construct 和查询函数开始。查询将返回一个您可以获取的 PDOStatement 对象。您的代码应如下所示:
// Connect
$host = '127.0.0.1';
$db = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';
$options = [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::ATTR_EMULATE_PREPARES => false,
];
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$pdo = new \PDO($dsn, $user, $pass, $options);
// Query
$statement = $pdo->query("SELECT * FROM your_table WHERE ID = 1");
// Fetch
$data = $statement->fetchAll();
foreach ($data as $row) {
echo $row['ID'].', '.$row['name'].', '.$row['PICTURE']."<br />\n";
}
- 1 回答
- 0 关注
- 60 浏览
添加回答
举报