PHP MySQL 读取数据

PHP MySQL 读取数据

从 MySQL 数据库读取数据

SELECT 语句用于从数据表中读取数据:

SELECT column_name(s) FROM table_name

如需学习更多关于 SQL 的知识,请访问我们的 SQL 教程。

以下实例中我们从表 MyGuests 读取了 id, firstname 和 lastname 列的数据并显示在页面上:

实例 (MySQLi - 面向对象)

connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出每行数据 while($row = $result->fetch_assoc()) { echo "
id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"]; } } else { echo "0 个结果"; } $conn->close(); ?>

以下实例读取了 MyGuests 表的所有记录并显示在 HTML 表格中:

实例 (PDO)

"; echo "IdFirstnameLastnameEmailReg date"; class TableRows extends RecursiveIteratorIterator { function __construct($it) { parent::__construct($it, self::LEAVES_ONLY); } function current() { return "" . parent::current(). ""; } function beginChildren() { echo ""; } function endChildren() { echo "" . "\n"; } } $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDBPDO"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $conn->prepare("SELECT * FROM MyGuests"); $stmt->execute(); // 设置结果集为关联数组 $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) { echo $v; } $dsn = null; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); } $conn = null; echo ""; ?>


继续学习
||
connect_error) { die("连接失败: " . $conn->connect_error); } $sql = "SELECT id, firstname, lastname FROM MyGuests"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出每行数据 while($row = $result->fetch_assoc()) { echo "
id: ". $row["id"]. " - Name: ". $row["firstname"]. " " . $row["lastname"]; } } else { echo "0 个结果"; } $conn->close(); ?>
提交 重置代码
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!