database链接检测
```
$dbhost = 'localhost'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysqli_error());
}
echo '数据库连接成功!';
mysqli_close($conn);
```
SQL查询
```
$dbhost = 'localhost:3306'; // mysql服务器主机地址
$dbuser = 'root'; // mysql用户名
$dbpass = '123456'; // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('连接失败: ' . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
echo '数据库连接成功!';
$sql = 'SELECT username, email
FROM users';
mysqli_select_db( $conn, 'test' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
die('无法读取数据: ' . mysqli_error($conn));
}
echo '
mysqli_fetch_array demo
';
echo '
Username |
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
echo "
{$row['username']} "."
{$row['email']} "."
";}
echo '
';mysqli_close($conn);
```