php 数据库高级操作

数据库高级操作
(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值