我是php新手, 今天测试的时候碰到一个问题:<?php require_once('db_fns.php');

$conn = db_connect();

$result = $conn->query('select * from st');

if(!$result){
throw new Exception( mysql_errno().":".mysql_error());
}

?>这段代码里我的数据库根本没有 st 表,但是出错后  mysql_errno().":".mysql_error() 这段信息却显示不出来, 用echo也显示不出来,请问是什么原因呀? 我用的wamp,是有什么地方的配置不对吗? php.ini里的 error_reporting 也设置的是 E_ALL,崩溃了...

解决方案 »

  1.   

    首先请确认你的mysql安装好了且能够连接上。设置E_ALL是设置的报错级别。
    display_errors不打开的话也不会有错误提示的。
    而且你的代码中,表不存在是fetal error,你抛出了异常却未截获。
    看下有输出不?没有输出的
    <?php
        $conn = mysql_connect('localhost','root','554981044');
        mysql_select_db("test");
        
        try{
      $result = mysql_query('select * from st');
          if(!$result){
            throw new Exception( mysql_errno().":".mysql_error());
          }
    }catch( Exception $e){
      echo $e->getMessage();
    }
      

  2.   

    你好,谢谢回答,把代码改成这样:<?php require_once('db_fns.php');

    $conn = db_connect();
    try{
    $result = $conn->query('select * from st');

    if(!$result){
    echo mysql_errno().":".mysql_error();
    throw new Exception( mysql_errno().":".mysql_error());

    }
    }catch( Exception $e){
          echo $e->getMessage();
      }
    ?>结果就是输出了两个 冒号... 晕了
      

  3.   


    我这边可以正常显示  1146:Table 'test.st' doesn't exist。
    ini_set('display_errors',1);
    代码前面加上这句,看是什么错误。不过既然你前面用 ->query()这种对象方式,这里为什么用mysql_error();或者mysql_erron呢?改为 ->error;
         ->erron这种形式
      

  4.   


    也就是改为这种代码形式<?php    require_once('db_fns.php');
        
        $conn = db_connect();
        try{
            $result = $conn->query('select * from st');
            
            if(!$result){
                echo $conn->error.":".$conn->erron;
                throw new Exception( $conn->erron.":".$conn->error);
                
            }
        }catch( Exception $e){
          echo $e->getMessage();
      }
    ?>
      

  5.   

    你使用了自定义的数据库类,如何知道该类一定是连接的 mysql 呢?
    况且类中可能已经对错误进行了处理,在发生错误时关闭了连接
      

  6.   

    谢谢上面两位的回答:把数据库类也贴出来:<?phpfunction db_connect()
    {
       $result = new mysqli('localhost', 'root', '111111', 'coldstorage'); 
       if (!$result)
         throw new Exception('Could not connect to database server');
       else
         return $result;
    }?>就是用mysqli 连数据库的...
      

  7.   


    ....
    既然是mysqli连接的。为啥用mysql_error()
    ...试试我5L的代码。
      

  8.   

    to:ohmygirl谢谢 谢谢, 非常感谢   <?php    require_once('db_fns.php');
        
        $conn = db_connect();
        try{
            $result = $conn->query('select * from st');
            
            if(!$result){
                echo $conn->error.":".$conn->erron;
                throw new Exception( $conn->erron.":".$conn->error);
                
            }
        }catch( Exception $e){
          echo $e->getMessage();
      }
    ?>
    这种方式可以看到错误信息  ,太谢谢了, 看来php5增加的 mysqli 查看错误信息的方式也变了, 我在google上搜“ mysql_error() 不能显示错误信息” 死活找不到答案,原来关键字都错了...
      

  9.   


    mysqli是mysql的高版本的扩展库。用对象的访问方式跟mysql的是几乎一致的。