php 检查有效的 MySQL 结果资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/783125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking for valid MySQL result resource
提问by Svish
I have this code:
我有这个代码:
$rows = array();
$res = mysql_query($someQuery);
if(!mysql_errno())
while($row = mysql_fetch_assoc($res))
$rows[] = $row;
$someQueryis 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".
$someQuery是我写入表单的任意查询。当我编写一个包含错误的 mysql 查询时,mysql_errno 会捕捉到这种情况。但是,我刚刚发现,当我执行“从 table_name 中删除”查询时,它当然不是错误,但同时 mysql_fetch_assoc 失败并显示“警告:mysql_num_rows():提供的参数不是有效的 MySQL 结果/blah/blah/file.php 第 x 行中的资源”。
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?
我试图在手册中寻找它(也许我只是瞎了……)但是有没有一个函数可以用来检查 $res 是否是有效的 MySQL 结果资源?
回答by Matt
if ($res)should work fine to check if it's a resource. is_resource()will determine if its a valid resource at all.
if ($res)检查它是否是资源应该可以正常工作。 is_resource()将确定它是否是一个有效的资源。
You could also check mysql_affected_rowsto try to determine if it's an INSERT/UPDATE/etc
您还可以检查mysql_affected_rows以尝试确定它是否是 INSERT/UPDATE/etc
回答by Caner
Along with is_resource()you can use get_resource_type()to check whether it is a MySQL resource.
连同is_resource()你可以get_resource_type()用来检查它是否是一个 MySQL 资源。
$res_type = is_resource($res) ? get_resource_type($res) : gettype($res);
if(strpos($res_type, 'mysql') === false) {
echo 'Invalid resource type: ' . $res_type;
}
get_resource_type()may return "mysql link"or "mysql link persistent"depending on your connection type.
get_resource_type()可能会返回"mysql link"或"mysql link persistent"取决于您的连接类型。
回答by Vladimir
Check http://www.lampdocs.com/blog/2010/10/how-to-check-that-a-php-variable-is-a-mysql-resource/for the solution. Hope this helps.
检查http://www.lampdocs.com/blog/2010/10/how-to-check-that-a-php-variable-is-a-mysql-resource/以获得解决方案。希望这可以帮助。
回答by Lennart Koopmann
mysql_query()returns trueor falseso you can check it this way:
mysql_query()返回true或者false你可以这样检查:
if($res) {
// The query succeeded.
}
回答by Randy
Perhaps just change the condition to:
也许只是将条件更改为:
if(!mysql_errno() && @mysql_num_rows($res) > 0)
The conditional will fail if there's no rows, and @ will suppress the warning.
如果没有行,条件将失败,@ 将取消警告。
回答by BrynJ
If you INSERT, UPDATE, DELETE or DROP via mysql_querythen it will only return trueor false(depending on success of operation).
如果您通过 INSERT、UPDATE、DELETE 或 DROP 插入,mysql_query那么它只会返回trueor false(取决于操作的成功)。
I'm not sure what you are expecting the resource to contain in this instance?
我不确定您希望资源在此实例中包含什么?
If you need the number of affected rows, you can use mysql_affected_rows().
如果需要受影响的行数,可以使用mysql_affected_rows().

