php mysql 数据表获取字段名,长度,信息
php可以利用mysql交互的相关函数可以获取数据表的字段信息,如可以获取数据表字段名,字段长度,字段信息等.
php mysql 数据表获取字段名,长度,信息实例代码如下:
<?php
$hostname="localhost"; //定义连接到的mysql服务器名
$username="root"; //定义用于连接的用户名
$password=""; //定义用于连接的密码
$link=mysql_connect($hostname,$username,$password); //打开mysql连接
$db_list=mysql_list_dbs($link); //列出数据库教程
$rows=mysql_num_rows($db_list); //取得返回结果数
$i=0;
while($i<$rows) //通过循环遍历结果集并赋值给对象
{
echo mysql_db_name($db_list,$i)."\n"; //输出对象内容
echo "<p>\n";
$i++;
}
mysql_close($link); //关闭mysql连接
//返回列的长度
$sql_str="select * from friends where id=1"; //定义sql语句
$result=mysql_query($sql_str); //执行sql语句
$re_a=mysql_fetch_array($result);
$re_len=mysql_fetch_lengths($result);
for($i=0;$i<count($re_len);$i++)
{
echo "返回结果的第".$i."列的长度为:".$re_len[$i];
echo "<p>";
}
mysql_close($link);
//获取字段信息
$result=mysql_query("select * from friends"); //执行sql查询
/*获取字段信息*/
$i=0;
while($i<mysql_num_fields($result)) //循环读取结果数
{
$i++;
echo "第".$i."列的信息:<br/>\n";
$meta=mysql_fetch_field($result); //获取字段信息
if(!$meta) //如果值不存在
{
echo "no information available<br/>\n"; //输出无可用信息
}
echo "<pre class="brush:php;toolbar:false">
blob: $meta->blob
max_length: $meta->max_length
multiple_key: $meta->multiple_key
name: $meta->name
not_null: $meta->not_null
numeric: $meta->numeric
primary_key: $meta->primary_key
table: $meta->table
type: $meta->type
unique_key: $meta->unique_key
unsigned: $meta->unsigned
zerofill: $meta->zerofill
"; $re_name=mysql_field_name($result,1); //获取第二个字段的名称 echo "第二个字段的名称为:".$re_name; echo "
";//开源代码phprm.com $re_name=mysql_field_name($result,2); //获取第三个字段的名称 echo "第三个字段的名称为:".$re_name; echo "
"; $re_name=mysql_field_name($result,3); //获取第四个字段的名称 echo "第四个字段的名称为:".$re_name; echo "
"; $re_name=mysql_field_name($result,4); //获取第五个字段的名称 echo "第五个字段的名称为:".$re_name; echo "
";
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1385
52

