• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

MySQL数据库函数详解(3)

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 8次 | 进入软件测试论坛讨论

领测软件测试网 作者:随想 OSO奥索 (9) string mysql_error(int [link_id]);
对于给定的连接,返回含有最近返回状态的与MySQL相关的函数的错误消息字符串。空值意味着未出现错误。

<?php
$link=mysql_pconnect("localhost","sunsoft","suixiang") or die("Could not connect");
print("Connected successfully");
$query="SELECT * FROM president";
$result=mysql_query($query) or die("query failed,error message=".mysql_error());
?>

(10)array mysql_fetch_array(int result, int [result_typ]);
本函式用来将查询结果 result 拆到阵列变数中。若 result 没有资料,则传回 false 值。而本函式可以说是 mysql_fetch_row() 的加强函式,除可以将传回列及数字索引放入阵列之外,还可以将文字索引放入阵列中。若是好几个传回栏位都是相同的文字名称,则最后一个置入的栏位有效,解决方法是使用数字索引或者为这些同名的栏位 (column) 取别名 (alias)。值得注意的是使用本函式的处理速度其实不会比mysql_fetch_row() 函式慢,要用哪个函式还是看使用的需求决定。参数 result_typ 是一个常数值,有以下几种常数 MYSQL_ASSOC、MYSQL_NUM 与 MYSQL_BOTH。
使用范例


<?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_array($result))
{
printf("%s %s<BR>
",$row[0],$row[1]);
printf("%s %s<BR>
",$row["last_name"],$row["first_name"]);
}
mysql_free_result($result);

?>


(11) object mysql_fetch_field(int result [,int col_num]);
返回结果集中给定列的相关元数据信息,如果没有这样的列,则返回假。如果省略col_num,则对mysql_fetch_field()的后继调用返回结果集后续列的信息。如果不再有剩余的列。则返回值为假。如果指定了col_num,则其取值范围为0到mysql_num_fields()-1。在此情况下,mysql_num_fields()返回给定列的相关信息,如果col_num超出范围,返回假。
<?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);
$meta=mysql_fetch_field($result);
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>
");
}
?>

(12) array mysql_fetch_lengths(int result);
本函式将 mysql_fetch_row() 处理过的最后一列资料的各栏位资料最大长度放在阵列变数之中。若执行失败则传回 false 值。传回阵列的第一笔资料索引值是 0。

<?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" or die("Query failed");
$row_num=0;
while(mysql_fetch_row($result))
{
++$row_num;
printf("Lengths of values in row %d:<BR>
",$row_num);
$len=mysql_fetch_lengths($result);
if(!$len)
{
print("No information available<BR>
");
break;
}
print("<PRE>
");
for($i=0;$i<mysql_num_fields($result);$i++)
printf("Column %d: %s
",$i,$len[$i]);
print("</PRE>
");
}
?>

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网