1. 建立数据库连接
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","","mydb");
?>
复制代码 代码如下:
$mysqli = new mysqli();
$mysqli->connect("localhost","root","","mydb");
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
}
else
{
echo "The Connection is Error!";
exit();
}
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","rootsss","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
echo "Result row nums:".$result->num_rows."
"; //显示结果集数量
//迭代结果集
while(list($id,$name,$age,$address) = $result->fetch_row())
{
echo "$id : $name : $age : $address"."
";
}
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
$sql = "SELECT * FROM student";
$result = $mysqli->query($sql);
echo "Result row nums:".$result->num_rows."
"; //显示结果集数量
//迭代结果集
while($rowObject = $result->fetch_object())
{
echo "$rowObject->id : $rowObject->name : $rowObject->age : $rowObject->address"."
";
}
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>
复制代码 代码如下:
while($row = $result->fetch_array(MYSQLI_ASSOC)) //返回关联数组
{
echo $row['id']. $row['name'].$row['age'].$row['address']."
";
}
?>
复制代码 代码如下:
while($row = $result->fetch_array(MYSQLI_NUM)) //返回索引数组
{
echo $row[0]. $row[1].$row[2].$row[3]."
";
}
?>
复制代码 代码如下:
...
$result->free(); //释放内存
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
$sql = "INSERT INTO student(id,name,age,address) VALUES('8','kay','23','xian')";
$result = $mysqli->query($sql);
echo $mysqli->affected_rows; //输出影响的行数
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>
复制代码 代码如下:
...
$mysqli->close();
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
$sql = "INSERT INTO student(id,name,age,address) VALUES(?,?,?,?)";
$stmt = $mysqli->stmt_init(); //创建预处理对象
$stmt->prepare($sql); //对SQL进行预处理
$stmt->bind_param("isis",$id,$name,$age,$address); //设置绑定的变量 第一个参数为变量的数据类型
for($i = 12;$i{
$id = $i + 1;
$name = "樊凯";
$age = 23;
$address = "xian";
$stmt->execute(); //执行SQL语句
}
echo $mysqli->affected_rows; //输出影响的行数
$stmt->close(); //释放预处理对象占用的内存
$mysqli->close(); //关闭数据库连接
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>
复制代码 代码如下:
$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{
$sql = "SELECT * FROM student";
$stmt = $mysqli->stmt_init(); //创建预处理对象
$stmt->prepare($sql); //对SQL进行预处理
$stmt->bind_result($id,$name,$age,$address); //将查询结果字段绑定到变量中
$stmt->execute(); //执行SQL语句
while($stmt->fetch()) //fetch方法用于获取结果集中的每一行,并将相应字段值赋给变量
{
echo "$id : $name : $age : $address"."
";
}
$stmt->close(); //释放预处理对象占用的内存
$mysqli->close(); //关闭数据库连接
}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>