PHP-mysql_num_rows()期望参数1为资源,给定布尔值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-mysql_num_rows()期望参数1为资源,给定布尔值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5554字,纯文字阅读大概需要8分钟。
内容图文
![PHP-mysql_num_rows()期望参数1为资源,给定布尔值](/upload/InfoBanner/zyjiaocheng/888/2f5f4ec9ca4a4e62a34f70181d213163.jpg)
这个问题已经在这里有了答案: > mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 32个
我知道这里有很多问题,我一直在寻找解决方案的时间,除了看不到应将mysql_query更改为mysqli_query之外,我似乎似乎找不到我的代码有什么问题,但是当我更改它时给了我更多错误消息,我不知道现在还能尝试什么.如果可以的话,请帮忙,这开始占用我很多时间.谢谢! (不要批评说“有很多同样的问题”,我认为每个人最终都会发布他们的问题,因为他们无法找到问题的答案,因为它与其他人不匹配,至少在我而言,我询问之前没有成功.)
// try get "MySQL link identifier"
$dbConn = mysql_connect($serverName, $user_name, $password) or die("Cannot connect to server<br />\n MySQL error ==>" . mysql_errno() . "<== : ==>" . mysql_error() . "<== <br />\n");
print "";
// try connect to database
mysql_select_db($db_name, $dbConn) or die("Cannot connect to database<br />\n MySQL error ==>" . mysql_errno($dbConn) . "<== : ==>" . mysql_error($dbConn) . "<== <br />\n");
print "<br />\n";
// Retrieve data from database
$id = $_GET['id'];
$additional_notes = $_GET['additional_notes'];
// update data in mysql database
$sql=("UPDATE `rmstable2` SET `additional_notes` = '$additional_notes' WHERE `id` = '$id'");
$result = mysql_query($sql) or die(mysql_error());
$resultcount = mysql_affected_rows($result);
if ($resultcount == 1) {
mysql_query("UPDATE `rmstable2` SET `additional_notes` = '$additional_notes' WHERE `id` = '$id'") or die(mysql_error());
}
// if successfully updated.
if($result)
{
echo "Update Successful!";
echo '<h3>Your case has been updated.</h3>';
echo "To see your changes please click <a href='/fullcase.php?id=$id'>here</a></b>";
}
else {
echo "ERROR";
}
?>
解决方法:
当传递UPDATE查询时,mysql_query()返回boolean TRUE表示成功,否则返回FALSE表示失败,而mysql_num_rows()仅接受结果集资源作为其参数.为了确定UPDATE查询影响了多少行,请以连接资源作为参数调用mysql_affected_rows().
它并没有引起您现在遇到的问题,但是非常建议您在mysql_query()调用中附加或死掉(mysql_error()),以便捕获发生的任何MySQL错误.甚至建议您完全放弃mysql_ *函数,而转而使用PHP PDO扩展,如PHP手册中所建议的那样,并且这样做实际上不会招致更多的认知开销,以换取它在功能和性能上的巨大好处.安全.
抛开这些,下面是我如何更改您的代码,使其行为更像您所想的那样:
<?php
// obtain a database connection
$dbConn = mysql_connect($serverName, $user_name, $password)
or die("Cannot connect to server: " . mysql_error() . "<br />\n");
// mysql error number rarely adds enough information to be worth including
// select the database
mysql_select_db($db_name, $dbConn)
or die("Couldn't select $db_name: " . mysql_error() . "<br />\n");
// obtain escaped versions of query data for inclusion in update query
// it is imperative to use mysql_real_escape_string() or equivalent if you're
// going to use mysql_* functions instead of the far preferable PDO
// prepared statements; if you don't escape your data, you leave open the
// possibility of SQL injection, which someone will certainly soon use to
// screw up your website horribly
$id = mysql_real_escape_string($_GET['id']);
$additional_notes = mysql_real_escape_string($_GET['additional_notes']);
// assemble query to pass to mysql_query()
// no need for parentheses around the string; in fact i'm surprised that
// didn't result in a parse error
// also FYI re backticks, MySQL uses them to denote literal database/table/
// column names -- they're optional unless required to disambiguate between
// an entity name and a reserved word. for example, you can create a table
// containing a column named 'key', which is a MySQL reserved word, but you
// thereafter must refer to that column as `key`, with backticks, in any
// queries, to hint to MySQL's parser that you mean the column by that name
// and not the reserved word; otherwise, it's a parse error.
$sql = "UPDATE `rmstable2` SET `additional_notes` = '$additional_notes' WHERE `id` = '$id'";
// actually run the query
// this being an UPDATE query, the result is boolean and offers no
// additional useful information, so you need not capture it in a variable;
// the 'or die' clause will fire if it's false, and if it's true, you'll
// use mysql_affected_rows() to get the additional info you need.
mysql_query($sql)
or die(mysql_error());
// if the query failed, the script die()d on the previous line and didn't
// get here; if it did get here, you know the query succeeded
$resultcount = mysql_affected_rows($dbConn);
// this is technically correct but semantically odd; since you already included
// the 'additional_notes' value in the previous UPDATE query, and since
// that query certainly succeeded if we're evaluating this code at all,
// why run the same query again?
if ($resultcount == 1) {
mysql_query("UPDATE `rmstable2` SET `additional_notes` = '$additional_notes' WHERE `id` = '$id'")
or die(mysql_error());
}
// again, the 'or die' clauses mean that we can only have reached this point
// if the queries succeeded, so there's no need for an if() test here
echo "Update Successful!";
echo '<h3>Your case has been updated.</h3>';
// note the backslashes before the embedded double quotes; single quotes in
// tag attributes are technically invalid but most browsers will accept them,
// but you can use double quotes within a double-quoted string if you precede
// the embedded quotes with backslashes (called "escaping") to indicate that
// they're not to be taken as the end of the string
// (i.e., "\"\"" == '""')
echo "To see your changes please click <a href=\"/fullcase.php?id=$id\">here</a></b>";
?>
内容总结
以上是互联网集市为您收集整理的PHP-mysql_num_rows()期望参数1为资源,给定布尔值全部内容,希望文章能够帮你解决PHP-mysql_num_rows()期望参数1为资源,给定布尔值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。