今天学习了下php的数据库操作。
$conn = mysqli_connect("localhost","root","root","database");
$results = $conn->query($sql);
用法:
while( $row=$results->fetch_row())
{
print($row[0] . " ". $row[1] . " ".$row[2]. "<br>"); // 0 1 2列
}
<?php
$conn= mysqli_connect("localhost","root","26841397","onlinejudge"); //创建连接对象
if(empty($conn))
die("error:".mysqli_connect_error()); //die 等同于exit();
$sql="CREATE TABLE IF NOT EXISTS TEST(UserName VARCHAR(50) PRIMARY KEY, UserPwd VARCHAR(50),
ShowName VARCHAR(50))";
mysqli_query($conn,"SET NAMES gb2312");
$conn->query($sql);
$sql="SELECT userName , userID , userSex FROM userInformation";
$results=$conn->query($sql);
while($row=$results->fetch_row()) //获取一行数据
print($row[0] . " ". $row[1] . " ".$row[2]. "<br>"); // 0 1 2列
$results->free();
mysqli_close($conn); //关闭对象
?>