<?php
$host = 'localhost';
$user = 'root';
$password = '8438031100';
$database = 'Phpitem';
$port = '3306';
$connect = mysqli_connect($host, $user, $password, $database, $port) or die("连接数据库服务器失败!" . mysqli_errno());
if ($connect) {
echo '连接成功' . "<br>";
mysqli_query($connect, "set names utf8");
$result = mysqli_query($connect, "select * from student");
/*
$array1 = mysqli_fetch_array($result);
while ($array1) {
echo $array1['uname'] . "<br>";
}这样子就死循环,必须$array = mysqli_fetch_array($result),它一行一行的遍历
* */
/* $array = mysqli_fetch_array($result)第二个参数默认是mysql_both(支持数字为下标和表字段名为);
*
* */
/*
$array1 = mysqli_fetch_array($result); */
?>
<!-- <table style="border: 1px solid #4999dd">
<tr>
<td>//<?php echo "t" . $array1['sid'] ?></td>
<td>//<?php echo "t" . $array1[1] ?></td>
<td>//<?php echo "t" . $array1[2] ?></td>
<td>//<?php echo "t" . $array1[3] ?></td>
<td>//<?php echo "t" . $array1[4] ?></td>
<td>//<?php echo "t" . $array1[5] ?></td>
</tr>
</table>//结果集是不是只能遍历一次?
-->
<?php
//为何只显示一个结果?
while ($array = mysqli_fetch_array($result)) {
echo '123';
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $array['sid'] ?></td>
<td><?php echo "t" . $array[1] ?></td>
<td><?php echo "t" . $array[2] ?></td>
<td><?php echo "t" . $array[3] ?></td>
<td><?php echo "t" . $array[4] ?></td>
<td><?php echo "t" . $array[5] ?></td>
</tr>
</table>
<?php
}
?>
<?php
while ($array = mysqli_fetch_array($result)) {
echo '456';
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $array['sid'] ?></td>
<td><?php echo "t" . $array[1] ?></td>
<td><?php echo "t" . $array[2] ?></td>
<td><?php echo "t" . $array[3] ?></td>
<td><?php echo "t" . $array[4] ?></td>
<td><?php echo "t" . $array[5] ?></td>
</tr>
</table>
<?php
// echo $array['Uname'] . "<br>";区分大小写mysqli_fetch_object($result)和 mysqli_fetch_array($result)
}
} else {
echo '连接不成功';
}
<?php
header("content-type:text/html;charset=utf-8");
include_once 'conn.php';
?>
<?php
function test01() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
while ($rows = mysqli_fetch_array($result)) {
//这样遍历中文就会出乱码,必须再遍历之前设置数据库编码格式---->mysqli_query($conn, "set names utf8");就不会出现乱码了
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $rows['sid'] ?></td>
<td><?php echo "t" . $rows[1] ?></td>
<td><?php echo "t" . $rows[2] ?></td>
<td><?php echo "t" . $rows[3] ?></td>
<td><?php echo "t" . $rows[4] ?></td>
<td><?php echo "t" . $rows[5] ?></td>
</tr>
</table>
<?php
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
<!--mysqli_fetch_array($result)
mysqli_fetch_object($result)
都属性名都区分大小写,不能将sid写成Sid
-->
<?php
function test02() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
while ($rows = mysqli_fetch_object($result)) {
//这样遍历中文就会出乱码,必须再遍历之前设置数据库编码格式---->mysqli_query($conn, "set names utf8");就不会出现乱码了
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $rows->sid ?></td>
<td><?php echo "t" . $rows->uname ?></td>
<td><?php echo "t" . $rows->age ?></td>
<td><?php echo "t" . $rows->address ?></td>
<td><?php echo "t" . $rows->class ?></td>
<td><?php echo "t" . $rows->tel ?></td>
</tr>
</table>
<?php
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
<?php
//mysqli_fetch_row($result)---从结果集每次获取都能获取一行作为枚举数组----》下标只能是:数字
//mysqli_fetch_array($result)这个的数组下标可以是数字或者表的字段名(枚举数组和关联数组)
function test03() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
while ($rows = mysqli_fetch_row($result)) {
//这样遍历中文就会出乱码,必须再遍历之前设置数据库编码格式---->mysqli_query($conn, "set names utf8");就不会出现乱码了
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $rows[0] ?></td>
<td><?php echo "t" . $rows[1] ?></td>
<td><?php echo "t" . $rows[2] ?></td>
<td><?php echo "t" . $rows[3] ?></td>
<td><?php echo "t" . $rows[4] ?></td>
<td><?php echo "t" . $rows[5] ?></td>
<!--
<td><?php echo "t" . $rows[6] ?></td>下标越界就不可以,机会出现错误
-->
</tr>
</table>
<?php
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
<?php
//mysqli_fetch_assoc($result)---从结果集每次获取都能获取一行作为关联数组----》下标只能是:表中的字段名
//mysqli_fetch_array($result)这个的数组下标可以是数字或者表的字段名(枚举数组和关联数组)
function test04() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
while ($rows = mysqli_fetch_assoc($result)) {
//这样遍历中文就会出乱码,必须再遍历之前设置数据库编码格式---->mysqli_query($conn, "set names utf8");就不会出现乱码了
?>
<table style="border: 1px solid #4999dd">
<tr>
<td><?php echo "t" . $rows['sid'] ?></td>
<td><?php echo "t" . $rows['uname'] ?></td>
<!--
<td><?php echo "t" . $rows[6] ?></td>下标越界就不可以,机会出现错误
-->
</tr>
</table>
<?php
mysqli_free_result($result);
mysqli_close($conn);
}
}
?>
<?php
//$rows = mysqli_num_rows($result)---获取结果集总共有多少行
//mysqli_fetch_array($result)这个的数组下标可以是数字或者表的字段名(枚举数组和关联数组)
function test05() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
$rows = mysqli_num_rows($result);
echo '总共有' . $rows . '行记录哦';
mysqli_free_result($result);
mysqli_close($conn);
}
?>
<?php
//mysqli_free_result($result);---将结果集释放掉,节约内存,记得是结果集不是连接返回对象
//mysqli_close($conn);----->将数据库连接对象给释放掉或者关闭掉,节约内存,记得是数据库连接对象
function test06() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'select * from student';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
$rows = mysqli_num_rows($result);
echo '总共有' . $rows . '行记录哦';
mysqli_free_result($result);
}
?>
<?php
//添加数据
function test07() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'insert into student values(null,"刘德威",45,"陕西省湖南西安","大数据三班","15521195587")';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
//$rows = mysqli_num_rows($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
//echo '总共有' . $rows . '行记录哦';
if ($result) {
echo '成功添加数据---》1行记录哦';
} else {
echo '添加数据失败';
}
// mysqli_free_result($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
mysqli_close($conn);
}
?>
<?php
//修改数据
function test08() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'update student set uname="林科院" where sid=4';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
//$rows = mysqli_num_rows($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
//echo '总共有' . $rows . '行记录哦';
if ($result) {
echo '成功修改数据---》1行记录哦';
} else {
echo '修改数据失败';
}
// mysqli_free_result($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
mysqli_close($conn);
}
?>
<?php
//删除数据
function test09() {
global $conn; //这样是就是声明$conn是成员变量,如果没有这句话,就是局部变量,就会出错
$query = 'delete from student where sid=123135';
mysqli_query($conn, "set names utf8");
$result = mysqli_query($conn, $query);
//$rows = mysqli_num_rows($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
//echo '总共有' . $rows . '行记录哦';
if ($result) {
echo '成功删除数据---》1行记录哦';
} else {
echo '删除数据失败';
}
// mysqli_free_result($result);这句话出错,参数只能是结果集,也就是select查询语句,其他都出错
}
?>
<?php
test06();
echo '<br>';
test09();
echo '<br>';
test06();
版权声明:未标注转载均为本站原创,转载时请以链接形式注明文章出处。如有侵权、不妥之处,请联系站长删除。敬请谅解!
常见资源合集和破解 fmvvvteih...