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

          常用鏈接

          留言簿

          隨筆分類(75)

          隨筆檔案(78)

          相冊

          實用Links

          我的Links

          搜索

          •  

          積分與排名

          • 積分 - 114325
          • 排名 - 515

          最新評論

          閱讀排行榜

          評論排行榜

          數據庫高級操作
          (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 期待明天 閱讀(335) 評論(0)  編輯  收藏 所屬分類: PHP
          主站蜘蛛池模板: 石渠县| 晴隆县| 连云港市| 六盘水市| 安义县| 盖州市| 昆明市| 长海县| 彩票| 环江| 镶黄旗| 上犹县| 长丰县| 韶关市| 汕头市| 勐海县| 青龙| 玛沁县| 桑日县| 德阳市| 泰顺县| 吉林省| 武穴市| 霍州市| 贵南县| 荔浦县| 柳林县| 攀枝花市| 通河县| 沾益县| 诏安县| 晴隆县| 西乌珠穆沁旗| 会东县| 维西| 根河市| 任丘市| 鄂托克前旗| 三江| 柯坪县| 大同县|