《php和mysql web开发》笔记——第11章 使用MySQL从Web访问数据库
来源:互联网 发布:python汉化 编辑:程序博客网 时间:2024/06/06 02:22
建立连接:
mysqli::__construct ([ string$host =ini_get("mysqli.default_host") [, string$username= ini_get("mysqli.default_user") [, string$passwd= ini_get("mysqli.default_pw") [, string $dbname= "" [, int$port =ini_get("mysqli.default_port") [, string$socket= ini_get("mysqli.default_socket") ]]]]]] )
查询:
mixedmysqli::query ( string$query
[, int$resultmode
= MYSQLI_STORE_RESULT ] )
查询结果:
记录条数:$mysqli->num_rows
读取记录:array mysqli_result::fetch_assoc ( void )
断开连接:
void mysqli_result::free ( void )
bool mysqli::close ( void )
但是,这并不是必要的,因为脚本执行完毕的时候它们将被自动关闭。
插入,更新用的是也是query函数。可以通过$mysqli->affected_rows来获取影响的记录数。
<style>td,th{border:1px solid gray;}</style><?phpfunction showBooks(){$db = new mysqli('localhost', 'root', '111111', 'books');$query = "select * from books";if (mysqli_connect_errno()) {echo 'Error: Could not connect to database. Please try again later.';exit;}$result = $db->query($query);$numResult = $result->num_rows;echo "共".$numResult."条记录<br>";?><table style="border:1px solid gray;" cellspacing="0px"><tr><th>isbn</th><th>作者</th><th>标题</th><th>价格</th></tr><?phpfor($i=0;$i<$numResult;$i++){$row = $result->fetch_assoc();echo "<tr>";echo "<td>".$row['isbn']."</td>";echo "<td>".$row['author']."</td>";echo "<td>".$row['title']."</td>";echo "<td>".$row['price']."</td>";echo "</td>";}$result->free();?></table><?php}function showBooks2(){$db = new mysqli('localhost', 'root', '111111', 'books');$query = "select * from books";if (mysqli_connect_errno()) {echo 'Error: Could not connect to database. Please try again later.';exit;}$stmt = $db->prepare($query);$stmt->execute();$stmt->bind_result($isbn,$name,$title,$price);$stmt->store_result();$numResult = $stmt->affected_rows;echo "共".$numResult."条记录<br>";?><table style="border:1px solid gray;" cellspacing="0px"><tr><th>isbn</th><th>作者</th><th>标题</th><th>价格</th></tr><?phpwhile($stmt->fetch()){echo "<tr>";echo "<td>".$isbn."</td>";echo "<td>".$name."</td>";echo "<td>".$title."</td>";echo "<td>".$price."</td>";echo "</td>";}$stmt->close();?></table><?php}function insert($isbn,$name,$title,$price){$db = new mysqli('localhost', 'root', '111111', 'books');$query = "insert into books(isbn,author,title,price) values(?,?,?,?)";if (mysqli_connect_errno()) {echo 'Error: Could not connect to database. Please try again later.';exit;}$stmt = $db->prepare($query);$stmt->bind_param("sssd",$isbn,$name,$title,$price);$stmt->execute();if($stmt->error){echo $stmt->error;exit;}echo "成功插入".$stmt->affected_rows."条数据<br>";$stmt->close();}showBooks();insert("2015-10-09 03号","金庸","天龙八部",86.3);showBooks2();?>
0 0
- 《php和mysql web开发》笔记——第11章 使用MySQL从Web访问数据库
- 《php和mysql web开发》笔记——第10章 使用MySQL数据库
- 《php和mysql web开发》笔记——第8章 设计Web数据库
- 《php和mysql web开发》笔记——第9章 创建Web数据库
- 使用PHP从Web访问MySQL数据库
- 使用PHP从Web访问MySQL数据库
- 《php和mysql web开发》笔记——第3章 使用数组
- 《php和mysql web开发》笔记——第1章PHP快速入门
- 《php和mysql web开发》笔记——第6章 面向对象的PHP
- 《php和mysql web开发》笔记——第7章 错误和异常处理
- 《php和mysql web开发》笔记——第2章数据的存储与检索
- 《php和mysql web开发》笔记——第4章 字符串操作与正则表达式
- 《php和mysql web开发》笔记——第5章 代码重用与函数编写
- 使用PHP操作从Web访问MySQL
- PHP和MySQL Web开发读书笔记---使用mysql数据库
- PHP和MySQL WEB开发学习笔记
- 《PHP和MySQL Web应用开发》笔记
- PHP和MySQL Web开发读书笔记---创建Web数据库
- 如何使用反射技术获取泛型类的真实类型?
- java版txt文件下载解决
- jQuery学习日记-------第一篇(jQuery介绍)
- java中Set集合的遍历及实现类比较分析
- 键值判断单击双击和长按事件
- 《php和mysql web开发》笔记——第11章 使用MySQL从Web访问数据库
- 消息系统kafka及其变种Jafka、Metamorphosis (MetaQ)
- Android开发最佳实践
- 查找EMM和ECM
- centos 7 安装注意事项
- 双屏/多显示器截屏
- mfc下报OSG内存泄漏解决方法
- Erlang 编译时出现"no such file or directory" 与 当前工作目录问题
- 单引号引发的[object XMLHttpRequest]错误