作者:会唱歌的高跟鞋 | 来源:互联网 | 2023-05-18 22:53
--------------------面向对象
header("Content-type:text/html;charset=utf-8");
//mysqli操作mysql数据库(面向对象)
//1,创建mysqli对象
$mysqli = new mysqli('localhost', 'root', '1234', 'test');
if($mysqli->connect_error){
die("连接失败".$mysqi->connect_error);
}else{
echo "连接成功";
}
//中文编码
$mysqli->query("set names 'utf8'");
//2,操作数据库
$sql="select * from user1";
//res的结果集 .mysqli result
$res=$mysqli->query($sql);
//处理结果
while($row=$res->fetch_row()){
foreach($row as $key=>$val){
echo "---$val";
}
echo "
";
}
//关闭资源
$res->free();
//关闭连接
$mysqli->close();
?>
、、、、、、、、面向过程
header("Content-type:text/html;charset=utf-8");
//1,得到mysqli连接
$mysqli=mysqli_connect("localhost","root","1234","test");
if(!$mysqli){
die("连接失败".mysqli_connect_error($mysqli));
}
//2,向数据库发送sql语句
$sql="select * from user1";
mysqli_query($mysqli,"set names 'utf8'");
$res=mysqli_query($mysqli,$sql);
//3处理得到的结果
//循环取出$res中的数据
while($row=mysqli_fetch_row($res)){
foreach($row as $key=>$val){
echo "--$val";
}
echo "
";
}
msyqli_free_result($res);
mysqli_close($mysqli);
?>