随笔 - 78  文章 - 25  trackbacks - 0
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿

随笔分类(75)

随笔档案(78)

相册

实用Links

我的Links

搜索

  •  

积分与排名

  • 积分 - 112098
  • 排名 - 521

最新评论

阅读排行榜

评论排行榜

数据库高级操作
(1)获取报错信息mysql_error(),mysql_errno()
string mysql_error([resource $link_identifier]);返回上一个mysql函数的错误文本,如果没有出错则返回空字符串
int mysql_errno([resource $link_identifier]);返回上一个mysql函数的错误号码,如果没有出错则返回0
例如:选择一个不存在的数据库会出错,在一个已经存在的数据库中,操作一个不存在的表也会出错。

(2)获取数据库和表的信息
mysql_list_dbs(),
mysql_db_name(),
mysql_list_tables(),
mysql_tablename()
具体用法:
resource mysql_list_dbs([resource $link_identifier])
string mysql_db_name(resource $result,int $row[,mixed $field])
resource mysql_list_tables(string $databaseName[,resource $link_identifier])
string mysql_tablename(resource $result,int $i)
(3)
▼mysql_free_result()释放内存
▼mysql_num_rows()判断结果指针中所指记录的个数
<?php
    #连接数据库
    $link=mysql_connect("localhost","root","root");
    //选择数据库--不存在的数据库,出错
    mysql_select_db("noexistentdb");
    echo mysql_error()."<br>";

    mysql_select_db("rorely");
    #查询不存在的表,出错
    mysql_query("select * from noexistenttable");
    echo mysql_error()."<hr>";

    #输出数据库列表
    $db_list=mysql_list_dbs($link);
    while($row=mysql_fetch_object($db_list)) echo $row->Database."<br>";
    echo"<hr>";

    #输出数据库列表中所有数据库的名称
    $i=0;
    $count=mysql_num_rows($db_list);
    while($i<$count){
        echo mysql_db_name($db_list,$i)."<br>";
        $i++;
    }
    echo"<hr>";

    #输出数据库列表中所有的所有表mysql_list_tables(dbname)
    $result=mysql_list_tables("mysql");
    if(!$result) {
        print "DB error,could not list tables<br>";
        print "MySQL error:".mysql_error();
        exit;
    }
    while($row=mysql_fetch_row($result))
        print "Table:$row[0].<br>";
        
    mysql_free_result($result);
    echo"<hr>";

    #输出数据库表mysql_tablename(resource)
    $result=mysql_list_tables("rorely");
    for($i=0;$i<mysql_num_rows($result);$i++){
        printf("Table:%s<br>",mysql_tablename($result,$i));
    }
    mysql_free_result($result);
?>

输出结果如下:
Unknown database 'noexistentdb'
Table 'rorely.noexistenttable' doesn't exist
information_schema
mysql
rorely
test

information_schema
mysql
rorely
test

Table:columns_priv.
Table:db.
Table:event.
Table:func.
Table:general_log.
Table:help_category.
Table:help_keyword.
Table:help_relation.
Table:help_topic.
Table:host.
Table:ndb_binlog_index.
Table:plugin.
Table:proc.
Table:procs_priv.
Table:servers.
Table:slow_log.
Table:tables_priv.
Table:time_zone.
Table:time_zone_leap_second.
Table:time_zone_name.
Table:time_zone_transition.
Table:time_zone_transition_type.
Table:user.

Table:test
posted on 2009-07-02 09:02 期待明天 阅读(307) 评论(0)  编辑  收藏 所属分类: PHP

只有注册用户登录后才能发表评论。


网站导航: