mysqli链接数据库示例代码

时间:2022-06-27 04:42:39
$mysqli = new mysqli("localhost", "数据库用户名", "数据库密码", "数据库名称");

	/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
$mysqli->query("SET NAMES 'utf8'");
$result = $mysqli->query("SELECT * FROM 数据表 LIMIT 10"); /* Select queries return a resultset */
if ($result->num_rows > 0) { $row = $result->fetch_assoc();
echo $row["title"]; /* free result set */
$result->close();
} $mysqli->close();
<?php
header('Content-Type:text/html;Charset=utf-8');
$db = new mysqli();
$db->connect('localhost','root',0123,'test');
$db->set_charset('utf8');
$sql='select * from pw_luck';
$query=$db->query($sql); echo '<pre>';
while($rs=mysqli_fetch_array($query)){
$result[]=$rs;
}
$i=0;
print_r($result);
echo '</pre><hr>';
foreach($result as $key=>$value){
//if($i%2==0){
echo $key.'<->'.$value[0].'<->'.$value[1].'<->'.$value[2].'<->'.$value[3].'<hr>';
//}
$i+=1;
}
$query->free();
$db->close();