MySQL数据库函数详解(4)
程序员文章站
2022-04-04 11:36:58
...
本函式用来将查询结果 result 拆到物件变数中。使用方法和 mysql_fetch_array() 几乎相同,不同的地方在于本函式传回资料是物件而不是阵列。若 result 没有资料,则传回 false 值。另外值得注意的地方是,取回的物件资料的索引只能是文字而不能用数字,这是因为物件的特性。物件资料的特性中所有的属性(PRoperty) 名称都不能是数字,因此只好乖乖使用文字字串当索引了。参数 result_typ是一个常数值,有以下几种常数 MYSQL_ASSOC、MYSQL_NUM 与 MYSQL_BOTH。关于速度方面,本函式的处理速度几乎和mysql_fetch_row() 及 mysql_fetch_array() 二函式差不多,要用哪个函式还是看使用的需求决定。
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT last_name,first_name FROM president";
$result=mysql_query($query) or die("Query failed");
while($row=mysql_fetch_object($result))
printf("%s %s<BR>
",$row->last_name,$row->first_name);
mysql_free_result($result);
?>
(14) array mysql_fetch_row(int result);
作为一个数组返回给定结果集的下一行,如果没有更多的行,则返回假。列值可作为数组元素访问,在0到mysql_num_fields()-1范围内使用列索引。
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT last_name,first_name FROM president";
$result=mysql_query($query) or die("Query failed");
while($row=mysql_fetch_row($result))
printf("%s %s<BR>
",$row[0],$row[1]);
mysql_free_result($result);
?>
(15) string mysql_field_name(int result, int field_index);
返回结果集的给定列的名称。
col_num 的范围为0到mysql_num_fields()-1.
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT * FROM president";
$result=mysql_query($query) or die("Query failed");
for($i=0;$i<mysql_num_fields($result);$i++)
{
printf("Name of column %d:",$i);
$name=mysql_field_name($result,$i);
if(!$name)
print("No name available<BR>
");
else
print("$name<BR>
");
}
?>
(16) int mysql_field_seek(int result, int field_offset);
为随后的mysql_fetch_field()调用设置索引。发布没有明确列号的mysql_fetch_field()的下一次调用,将返回列col_num的信息。如果搜索成功,返回真,否则返回假。
col_num的范围为0到mysql_num_fields()-1.
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT * FROM president";
$result=mysql_query($query) or die("Query failed");
for($i=0;$i<mysql_num_fields($result);$i++)
{
printf("Information for column %d:<BR>
",$i);
if(!mysql_field_seek($result,$i))
{
print("Cannot seek to colum<BR>
");
continue;
}
$meta=mysql_fetch_field($result,$i);
if(!$meta)
{
print("No information available<BR>
");
continue;
}
print("<PRE>
");
printf("blob: %s
",$meta->blob);
printf("max_length: %s
",$meta->max_length);
printf("multiple_key: %s
",$meta->multiple_key);
printf("name: %s
",$meta->name);
printf("not_null: %s
",$meta->not_null);
printf("numeric: %s
",$meta->numeric);
printf("primary_key: %s
",$meta->primary_key);
printf("table: %s
",$meta->table);
printf("type: %s
",$meta->type);
printf("unique_key: %s
",$meta->unique_key);
printf("unsigned: %s
",$meta->unsigned);
printf("zerofill: %s
",$meta->zerofill);
print("</PRE>
");
}
?>
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT last_name,first_name FROM president";
$result=mysql_query($query) or die("Query failed");
while($row=mysql_fetch_object($result))
printf("%s %s<BR>
",$row->last_name,$row->first_name);
mysql_free_result($result);
?>
(14) array mysql_fetch_row(int result);
作为一个数组返回给定结果集的下一行,如果没有更多的行,则返回假。列值可作为数组元素访问,在0到mysql_num_fields()-1范围内使用列索引。
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT last_name,first_name FROM president";
$result=mysql_query($query) or die("Query failed");
while($row=mysql_fetch_row($result))
printf("%s %s<BR>
",$row[0],$row[1]);
mysql_free_result($result);
?>
(15) string mysql_field_name(int result, int field_index);
返回结果集的给定列的名称。
col_num 的范围为0到mysql_num_fields()-1.
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT * FROM president";
$result=mysql_query($query) or die("Query failed");
for($i=0;$i<mysql_num_fields($result);$i++)
{
printf("Name of column %d:",$i);
$name=mysql_field_name($result,$i);
if(!$name)
print("No name available<BR>
");
else
print("$name<BR>
");
}
?>
(16) int mysql_field_seek(int result, int field_offset);
为随后的mysql_fetch_field()调用设置索引。发布没有明确列号的mysql_fetch_field()的下一次调用,将返回列col_num的信息。如果搜索成功,返回真,否则返回假。
col_num的范围为0到mysql_num_fields()-1.
<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
mysql_select_db("stamp_db") or die("Could not select database");
$query="SELECT * FROM president";
$result=mysql_query($query) or die("Query failed");
for($i=0;$i<mysql_num_fields($result);$i++)
{
printf("Information for column %d:<BR>
",$i);
if(!mysql_field_seek($result,$i))
{
print("Cannot seek to colum<BR>
");
continue;
}
$meta=mysql_fetch_field($result,$i);
if(!$meta)
{
print("No information available<BR>
");
continue;
}
print("<PRE>
");
printf("blob: %s
",$meta->blob);
printf("max_length: %s
",$meta->max_length);
printf("multiple_key: %s
",$meta->multiple_key);
printf("name: %s
",$meta->name);
printf("not_null: %s
",$meta->not_null);
printf("numeric: %s
",$meta->numeric);
printf("primary_key: %s
",$meta->primary_key);
printf("table: %s
",$meta->table);
printf("type: %s
",$meta->type);
printf("unique_key: %s
",$meta->unique_key);
printf("unsigned: %s
",$meta->unsigned);
printf("zerofill: %s
",$meta->zerofill);
print("</PRE>
");
}
?>
以上就是MySQL数据库函数详解(4)的内容,更多相关内容请关注PHP中文网(www.php.cn)
上一篇: asp怎么连接sql数据库?
下一篇: mysql怎样查看帮助