首页 > 代码库 > mysqli操作mysql数据库(面向对象风格)

mysqli操作mysql数据库(面向对象风格)

//1.创建MySQLi对象

echo "<meta charset=‘utf-8‘>";

$mysqli=new MySQLi("localhost","root","root","test");

if($mysqli->connect_error){

die("连接失败".$mysqli->connect_error);

}

//2.操作数据库(发送sql)

$sql="select *from user1";

$res=$mysqli->query($sql);

//3.处理结果

while($row=$res->fetch_row()){

foreach($row as $key=>$val){

echo "--$val";

}

echo "<br/>";

}

//4.关闭资源,连接

$res->free();

$mysqli->close();



mysqli操作mysql数据库(面向对象风格)