一.mysql数据库
代码如下 | 复制代码 | |
$conn=mysql_connect("localhost", "root", "123456") or die("数据库连接失败");
mysql_close(); |
补充
代码如下 | 复制代码 | ||
// 从表中提取信息的sql语句 // 显示字段名称 |
二.pdo_mysql(推荐)
连接:
代码如下 | 复制代码 |
$pdo = new PDO($dsn, $username, $password, $options); 方法1: 绑定关联数组 $str=$pdo->prepare("INSERT INTO `user` (`username`, `password`) VALUES (:username,:password)"); 方法2: 绑定索引数组
改: $str=$pdo->prepare("UPDATE `user` SET username=:username,password=:password where id=:id"); $str=$pdo->prepare("select * from user where id > :id order by id"); 方法2: 全部取出,返回到二维数组
方法3:单个取出,循环遍历,绑定字段名到变量
|
二.mysqli
代码如下 | 复制代码 | |
(用mysqli链接MYSQL数据库) (其他操作)
$mysqli=new mysqli("localhost", "root", "123456", "test");
$mysqli->close(); |
三种处理查询结果
-------------------------------------------------------
代码如下 | 复制代码 |
$sql = "select * from user"; $result = $mysqli->query($sql); (1)fetch_row() 返回索引数组 fetch_row() while(list($id,$name,$pwd)=$result->fetch_row()){ echo "id: ".$id." name:".$name." pwd:".$pwd." "; } (2)fetch_assoc() 返回关联数组 fetch_assoc() while ($row = $result->fetch_assoc()){ echo "id:".$row["userId"]." name:".$row["userName"]." pwd:".$row["password"]." "; } (3)fetch_object()返回对象 while($row = $result->fetch_object()){ echo "id:".$row->userId." name:".$row->uerName." pwd:".$row->password." "; } |