php mysqli 扩展库操作 mysql 的例子
本文分享下
php 使用 mysqli 扩展操作 mysql 数据库的几个例子,挺不错的,有需要的朋友
可以参考下,一定会有所帮助的。
<?php
header("Content-type: text/html;charset=utf-8");
//mysqli 操作 mysql 数据库(面向对象方式)
//1、创建 MySQLi 对象
$mysqli =new MySQLi("localhost","root","root","test");
if($mysqli->connect_error){
die("连接失败".$mysqli->connect_error);
}
//2、操作数据库(发送 sql)
$sql="select *from user1";
//3、处理结果
$res =$mysqli->query($sql);
//var_dump($res);
//fetch_assoc \fetch_array \fetch_object
while($row=$res->fetch_row()){
var_dump($row);
/* foreach($row as $val){
echo '--'.$val;
}
echo '<br/>';*/
}
//4、关闭资源
$res->free();
$mysqli->close();
?>
面向过程的例子:
<?php
header("Content-type: text/html;charset=utf-8");
$mysqli=mysqli_connect("localhost","root","root","test");
if(!$mysqli){
die("连接失败".mysqli_connect_error());
}
$sql="select *from user1";