PHP与MySQL(15)
程序员文章站
2022-05-11 17:20:24
PHP配置 extension_dir = "ext"extension=php_mysqli.dll 建立和断开链接 获取错误信息 获取错误码 获取错误信息 在单独的文件中存储链接信息 在必要时包含此文件 与数据库交互 获取数据 插入、删除或更新数据 释放查询内存 解析查询结果 ......
PHP配置
extension_dir = "ext"
extension=php_mysqli.dll
建立和断开链接
$mysqli = new mysqli();//实例化mysqli类 $mysqli -> connect("localhost","root","123");//链接数据库 $mysqli -> select_db("text");//选择text数据库 $mysqli -> close();//关闭链接
获取错误信息
- 获取错误码
$mysqli = new mysqli("localhost","root","123","test"); echo $mysqli -> errno;//没有错误返回0
- 获取错误信息
$mysqli = new mysqli("localhost","root","123");//实例化mysqli类 $mysqli -> select_db("text");//选择text数据库 if($mysqli -> errno){ echo $mysqli -> error;//Unknown database 'text' 没有text数据库 } $mysqli -> close();//关闭链接
- 在单独的文件中存储链接信息
//mysql.connect.php文件 <?php $mysqli = new mysqli("localhost","root","123","test"); ?>
- 在必要时包含此文件
<?php include "mysql.connect.php";//调用mysql.connect.php文件 ?>
与数据库交互
获取数据
$mysqli = new mysqli("localhost","root","123","test"); $query = "select id, name, age from xiu"; $result = $mysqli -> query($query,MYSQLI_STORE_RESULT); while(list($id,$name,$age) = $result -> fetch_row()){ printf("%d*%d*%d",$id,$name,$age); } $mysqli -> close();
插入、删除或更新数据
$mysqli = new mysqli("localhost","root","123","test"); $query = "alter table xiu add column birdate date"; $result = $mysqli -> query($query); echo $mysqli -> affected_rows; $mysqli -> close();
释放查询内存
解析查询结果
上一篇: CI中的分页
下一篇: php中一个字符占用几个字节?