php基本语言总结
1,
mysqli_fetch_row记录集获取
mysqli_fetch_row();//用来将查询结果的一行保存至索引数组;
mysqli_fetch_assoc();//用来将查询结果的一行保存至关联数组;
mysqli_fetch_array();//前2者的结合。
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); while($row=mysqli_fetch_row($result)) { print_r($row); } ?>
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); while($row=mysqli_fetch_row($result)) { echo "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5]" echo "<br/>" } ?>
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); while($row=mysqli_fetch_row($result)) { $cols=count($row); for($i=0;$i<$cols;$i++) { echo $row[$i]; if($i<$cols-1)echo ","; } echo "<br/>"; } ?>
2
mysqli_fetch_assoc记录集获取
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); while($row=mysqli_fetch_assoc($result)) { echo "row[stu_no],$row[stu_name],$row[sex],$row[birthdate],$row[telephone],$row[email]"; echo "<br/>"; } ?>
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); while($row=mysqli_fetch_assoc($result)) { foreach($row as $k=>$v) { echo $v; if($k!="email")echo ","; } echo "<br/>"; } ?>
3
mysqli_fetch_array记录集获取
while($row=mysqli_fetch_array($result)) { $cols=count($row)/2; for($i=0;$i<$cols;$i++) { echo $row[$i]; if($i<$cols-1)echo ","; } echo "<br/>"; } ?>
<?php $conn=mysqli_connect("localhost","root","111111","mydb") or die("数据库连接失败"); mysqli_query($conn,'set names utf8'); $sql="select * from student"; $result=mysqli_query($conn,$sql)or die("数据查询失败"); echo "<table border=1>"; echo "<tr><td>学号</td><td>姓名</td><td>性别</td><td>出生日期</td><td>电话号码</td><td>邮箱</td></tr>"; while($row=mysqli_fetch_array($result)) { echo "<tr>"; foreach($row as $k=>$v) { if(!is_numeric($k)){ echo "<td>$v</td>"; } } echo "</tr>"; } echo "</table>"; ?>