mysql_error() 不起作用

lwmonster 2011-10-12 03:49:30
我是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,崩溃了...
...全文
340 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
小小排骨V 2011-10-12
  • 打赏
  • 举报
回复
见贴就回,多向前辈学习。
ohmygirl 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 lwmonster 的回复:]

to:ohmygirl

谢谢 谢谢, 非常感谢 PHP code
<?php

require_once('db_fns.php');

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

if(!$result)……
[/Quote]

mysqli是mysql的高版本的扩展库。用对象的访问方式跟mysql的是几乎一致的。
lwmonster 2011-10-12
  • 打赏
  • 举报
回复
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() 不能显示错误信息” 死活找不到答案,原来关键字都错了...
ohmygirl 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 lwmonster 的回复:]

谢谢上面两位的回答:

把数据库类也贴出来:PHP code
<?php

function db_connect()
{
$result = new mysqli('localhost', 'root', '111111', 'coldstorage');
if (!$result)
throw new Exception('Could not connect t……
[/Quote]

....
既然是mysqli连接的。为啥用mysql_error()
...

试试我5L的代码。
lwmonster 2011-10-12
  • 打赏
  • 举报
回复
谢谢上面两位的回答:

把数据库类也贴出来:
<?php

function db_connect()
{
$result = new mysqli('localhost', 'root', '111111', 'coldstorage');
if (!$result)
throw new Exception('Could not connect to database server');
else
return $result;
}

?>


就是用mysqli 连数据库的...
xuzuning 2011-10-12
  • 打赏
  • 举报
回复
你使用了自定义的数据库类,如何知道该类一定是连接的 mysql 呢?
况且类中可能已经对错误进行了处理,在发生错误时关闭了连接
ohmygirl 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ohmygirl 的回复:]

引用 3 楼 lwmonster 的回复:

你好,谢谢回答,把代码改成这样:PHP code
[/Quote]

也就是改为这种代码形式

<?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();
}
?>
ohmygirl 2011-10-12
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lwmonster 的回复:]

你好,谢谢回答,把代码改成这样:PHP code
<?php

require_once('db_fns.php');

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

if(!$result){
……
[/Quote]

我这边可以正常显示 1146:Table 'test.st' doesn't exist。
ini_set('display_errors',1);
代码前面加上这句,看是什么错误。

不过既然你前面用 ->query()这种对象方式,这里为什么用mysql_error();或者mysql_erron呢?

改为 ->error;
->erron这种形式
lwmonster 2011-10-12
  • 打赏
  • 举报
回复
你好,谢谢回答,把代码改成这样:
<?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();
}
?>


结果就是输出了两个 冒号... 晕了


[Quote=引用 2 楼 ohmygirl 的回复:]

首先请确认你的mysql安装好了且能够连接上。

设置E_ALL是设置的报错级别。
display_errors不打开的话也不会有错误提示的。
而且你的代码中,表不存在是fetal error,你抛出了异常却未截获。
看下有输出不?

没有输出的
PHP code
<?php
$conn = mysql_connect('localhost','root','55498……
[/Quote]
ohmygirl 2011-10-12
  • 打赏
  • 举报
回复
首先请确认你的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();
}

Loop-break 2011-10-12
  • 打赏
  • 举报
回复
重装下

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧