<?php
$servername = "localhost";
$username = "root";
$password = "admin";
$dbname = "myDB"; // 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->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"]. "<br>";
}
} else {
echo "0 结果";
}
$conn->close();
?>

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB"; // 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
} $sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) {
// 输出数据
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 结果";
} mysqli_close($conn);
?>
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>Id</th><th>Firstname</th><th>Lastname</th></tr>"; class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
} function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
} function beginChildren() {
echo "<tr>";
} function endChildren() {
echo "</tr>" . "\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 id, firstname, lastname FROM MyGuests");
$stmt->execute(); // 设置结果集为关联数组
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn = null;
echo "</table>";
?>

吴裕雄--天生自然 PHP开发学习:MySQL 读取数据的更多相关文章

  1. 吴裕雄--天生自然 PYTHON3开发学习:JSON 数据解析

    import json # Python 字典类型转换为 JSON 对象 data = { 'no' : 1, 'name' : 'Runoob', 'url' : 'http://www.runoo ...

  2. 吴裕雄--天生自然 PHP开发学习:MySQL 预处理语句

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  3. 吴裕雄--天生自然 PHP开发学习:MySQL 插入多条数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  4. 吴裕雄--天生自然 PHP开发学习:MySQL 插入数据

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  5. 吴裕雄--天生自然 PHP开发学习:连接 MySQL、创建表

    <?php $servername = "localhost"; $username = "root"; $password = "admin& ...

  6. 吴裕雄--天生自然 PYTHON3开发学习:MySQL - mysql-connector 驱动

    import mysql.connector mydb = mysql.connector.connect( host="localhost", # 数据库主机地址 user=&q ...

  7. 吴裕雄--天生自然 JAVA开发学习:MySQL 连接

    CREATE TABLE `websites` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL DEFAULT '' ...

  8. 吴裕雄--天生自然 PHP开发学习:MySQL子句

    <?php $con=mysqli_connect("localhost","username","password","d ...

  9. 吴裕雄--天生自然 JAVA开发学习:java使用Eclipel连接数据库

    1:jar可到Mysql官网下载:地址Mysql 连接jar包.:https://dev.mysql.com/downloads/connector/j/如图,在下拉列表框中选择Platform In ...

随机推荐

  1. sql编程利器,Sql Prompt下载及安装方法

    Sql Prompt只能提示及其格式化用起来非常方便: 推荐网址:www.4-yecao.com 免费下载地址:http://download.csdn.net/detail/caizz520/455 ...

  2. 利用ThoughtWorks.QRCode生成二维码

    一.项目添加ThoughtWorks.QRCode.dll和System.Drawing.dll的引用 二.创建二维码公共处理类(QRCodeHandler.cs) /// <summary&g ...

  3. Django template配置

    TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...

  4. 使用Oracle VM VirtualBox安装CentOS 7.6操作系统

    使用Oracle VM VirtualBox安装CentOS 7.6操作系统                                                               ...

  5. Arduino --structure

    The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...

  6. oracle批量修改字段长度

    alter table 表名 modify (字段名1 字段类型1(长度1),字段名2 字段类型2(长度2)) alter table 表名 modify column_name varchar2(3 ...

  7. 【LeetCode】最长回文子串-中心扩展法

    [问题]给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 : 输入: "babad" 输出: "bab" 注意: ...

  8. python刷LeetCode:26. 删除排序数组中的重复项

    难度等级:简单 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外 ...

  9. 模块化CommonJs规范 part1

    CommonJS规范 来自<JavaScript 标准参考教程(alpha)>,by 阮一峰 1.概述 Node 应用由模块组成,采用 CommonJS 模块规范. 每个文件就是一个模块, ...

  10. POJ 1942:Paths on a Grid

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22918   Accepted: 5651 ...