<?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. 09.swoole学习笔记--进程事件

    <?php //进程数组 $workers=[]; //创建进程的数据量 $worker_num=; //创建启动进程 ;$i<$worker_num;$i++){ //创建单独新进程 $ ...

  2. React 学习笔记(1) 基础语法和生命周期

    参看:视频地址 简单搭建一个react-cli: 2. React.createElement() 将object转化为 React语法 import React from 'react' impor ...

  3. Django中出现no such table: django_session

    这个错误跟Session的机制有关, 既然要从Web服务器端来记录用户信息, 那么一定要有存放用户session id对应信息的地方才行. 所以,我们需要创建django_session表. Djan ...

  4. spring学习第7天(PCD以及切点表达式)

    1.PCD(PointCutDesigner) spring的aop只针对方法进行aop代理,而apectj联盟的aop比之更加强大,还可以针对字段等进行切面编程 1.1:execution,用的最多 ...

  5. .Net 题目

    1.简述public.private.protected.Internal修饰符的访问权限 public:公共成员,完全公开,没有访问限制. private:私有成员,在类的内部才可以访问. prot ...

  6. node —— 静态资源文件管理

    var http = require("http"); var url = require("url"); var fs = require("fs& ...

  7. 关于 python 中 虚拟环 virtualen境的操作

    python3.X安装和pip安装方法 pip install -i https://pypi.douban.com/simple XXX 1.安装virtualenv pip install vir ...

  8. 一文说透 Spring 循环依赖问题

    https://zhuanlan.zhihu.com/p/62382615 循环依赖发生的时机 Bean 实例化主要分为三步,如图: 问题出现在:第一步和第二步的过程中,也就是填充属性 / 方法的过程 ...

  9. Unity3d中渲染到RenderTexture的原理,几种方式以及一些问题

    超级搬运工 http://blog.csdn.net/leonwei/article/details/54972653 ---------------------------------------- ...

  10. Vmware 部分所学到的功能简写

    VMware  vSphere  Hypervisor 免费版esxi VMware vSphere Update Manager (6.5之后集成到了VCSA中) 在 vSphere 6.5 及更高 ...