面向对象风格
<?php
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','127.0.0.1');
define('DB_NAME',dbname);
//连接数据库
$mysqli=new mysqli(DB_HOST, DB_USER,DB_PASSWORD, DB_NAME);
if($mysqli->connect_errno){
echo '数据库连接失败:'.$mysqli->connect_error;
}
$sql='select * from table';
$result=$mysqli->query($sql);
//输出
foreach ($result as $value){
echo '<pre>';
var_dump($value);
echo '</pre>';
}
?>
面向过程风格
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','127.0.0.1');
define('DB_NAME',dbname);
$link=mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD, DB_NAME);
if(mysqli_connect_errno()){
echo '数据库连接失败'.mysqli_connect_error();
}
$sql="select * from table";
$result=mysqli_query($link,$sql);
foreach ($result as $value){
echo '<pre>';
var_dump($value);
echo '</pre>';
}