首页 技术 正文
技术 2022年11月11日
0 收藏 882 点赞 3,232 浏览 2251 个字

mysqlitest.php

<?php
//调用数据库的函数
function connetionsql(){
$conn=mysqli_connect("127.0.0.1",'root','','user');
if (!$conn) {
die("连接失败".mysqli_error());
}
mysqli_set_charset($conn,"utf8");
$sql="select * from user1";
$res=mysqli_query($conn,$sql);
// mysqli_affected_rows()返回前一次 MySQL 操作所影响的记录行数。
// $row=mysqli_affected_rows($conn);
// mysqli_num_fields() 返回结果集中字段的数量。
$col=mysqli_num_fields($res);
echo "<table border='1'><tr>";
// 取出字段名作为表名
for ($i=0; $i <$col ; $i++) {
// mysqli_fetch_field_direct() 从结果集中取得某个单一字段的 meta-data,并作为对象返回。
$file_name=mysqli_fetch_field_direct($res,$i);
// echo $file_name->name;
echo "<th>$file_name->name</th>";
}
echo "</tr>";
// mysqli_fetch_row() 从结果集中取得一行,并作为枚举数组返回。
while ($row=mysqli_fetch_row($res)) {
echo "<tr>";
for ($i=0; $i <$col ; $i++) {
echo "<td>$row[$i]</td>";
}
echo "</tr>";
}
echo "</table>";
}
connetionsql();
?>

实现结果:

【二十一】基于mysqli的表格数据练习

解析后的html源码:

<table border='1'>
<tr>
<th>id</th><th>name</th><th>password</th><th>email</th><th>age</th>
</tr>
<tr>
<td>4</td><td>huahua</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>huahua@qq.com</td><td>16</td>
</tr>
<tr>
<td>32</td><td>haha</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
</tr>
<tr>
<td>28</td><td>test</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
</tr>
<tr>
<td>31</td><td>嘿嘿</td><td>e10adc3949ba59abbe56e057f20f883e</td><td>test@qq.com</td><td>16</td>
</tr>
</table>

下面是在过程中熟悉的mysqli函数:

mysqli_fetch_fields()

        // mysqli_fetch_fields()    返回结果中代表字段的对象的数组。
$field_info=mysqli_fetch_fields($res);
var_dump($field_info);
//返回的是一个二位数组,如下:
// array(5) {
// [0]=> object(stdClass)#3 (13)
// { ["name"]=> string(2) "id"
// ["orgname"]=> string(2) "id"
// ["table"]=> string(5) "user1"
// ["orgtable"]=> string(5) "user1"
// ["def"]=> string(0) ""
// ["db"]=> string(4) "user"
// ["catalog"]=> string(3) "def"
// ["max_length"]=> int(2)
// ["length"]=> int(11)
// ["charsetnr"]=> int(63)
// ["flags"]=> int(49667)
// ["type"]=> int(3)
// ["decimals"]=> int(0)
// }
foreach ($field_info as $key =>$value) {
// echo "<br/>".$val->name;
echo "<br/>.-----";
echo $key;
echo "=========";
echo $value->name;
}

结果:

//数字为下标
.-----0=========id
.-----1=========name
.-----2=========password
.-----3=========email
.-----4=========age

mysqli_fetch_field()

        // mysqli_fetch_field() 函数从结果集中取得下一字段(列),并作为对象返回。
while ($field_info=mysqli_fetch_field($res)) {
echo "<br/>".$field_info->name;
}

结果:

id
name
password
email
age
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777