检查有效的MySQL结果资源 [英] Checking for valid MySQL result resource

查看:44
本文介绍了检查有效的MySQL结果资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此代码:

$rows = array();
$res = mysql_query($someQuery);

if(!mysql_errno())
    while($row = mysql_fetch_assoc($res))
        $rows[] = $row;

$someQuery是我写入表单的任意查询.当我编写一个带有错误的mysql查询时,mysql_errno抓住了这种情况.但是,我刚刚发现,当我执行从table_name删除"查询时,它当然不是错误,但是同时mysql_fetch_assoc失败并显示警告:mysql_num_rows():提供的参数不是有效的MySQL结果" x上/blah/blah/file.php中的资源".

$someQuery is an arbitrary query that I write in to a form. The mysql_errno catches the case when I write a mysql query with errors in it. But, I just discovered that when I do a "Delete from table_name" query, it of course is not an error, but at the same time the mysql_fetch_assoc fails with a "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /blah/blah/file.php on line x".

我试图在手册中寻找它(也许我只是盲目...),但是有没有我可以用来检查$ res是否是有效的MySQL结果资源的函数?

I've tried to look for it in the manual (maybe I'm just blind...) but is there a function I can use to check if $res is a valid MySQL result resource or not?

推荐答案

if ($res)应该可以正常工作,以检查它是否是资源. is_resource()将确定其是否完全有效.

if ($res) should work fine to check if it's a resource. is_resource() will determine if its a valid resource at all.

您还可以检查 mysql_affected_rows 来确定它是否为INSERT/UPDATE/etc

You could also check mysql_affected_rows to try to determine if it's an INSERT/UPDATE/etc

这篇关于检查有效的MySQL结果资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆