php mysqli_affected_rows() 期望参数 1 是 mysqli,给定的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9456873/
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
mysqli_affected_rows() expects parameter 1 to be mysqli, object given
提问by BrokenCode
I am working on making server side validation for a form. Using AJAX, the form sends the value in the input field for 'username' to my php page which then checks to see if this username already exists in the database.
我正在为表单进行服务器端验证。使用 AJAX,表单将“用户名”的输入字段中的值发送到我的 php 页面,然后检查该用户名是否已存在于数据库中。
Here is my php code:
这是我的php代码:
$result = mysqli_query($dblink, "SELECT * FROM users WHERE `username` = '$regname'")
or die(mysqli_error($dblink));
echo mysqli_affected_rows($result);
*(At the moment I am doing a simple echo for the mysqli_affected_rows just to see if my MySQL query is working as intended)*
*(目前我正在对 mysqli_affected_rows 做一个简单的回显,只是为了查看我的 MySQL 查询是否按预期工作)*
The error I am getting is:
我得到的错误是:
Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, object given in /Users/test/Sites/proj/formvalidate.php on line 20
警告:mysqli_affected_rows() 期望参数 1 是 mysqli,在第 20 行的 /Users/test/Sites/proj/formvalidate.php 中给出的对象
I am not quite sure what this error is trying to tell me. From what I have Googled "object" is a reference to OOP programming methods, but (as far as I know) I am not using OOP concepts/principles in this particular example? Or did I misinterpret this error message?
我不太确定这个错误试图告诉我什么。从我在 Google 上搜索到的“对象”是对 OOP 编程方法的引用,但是(据我所知)在这个特定示例中我没有使用 OOP 概念/原则?还是我误解了这个错误信息?
Thank you.
谢谢你。
回答by pjumble
Rather than passing $result
in to mysqli_affected_rows
you actually want to pass the DB link (returned by mysqli_connect
) which will give you the number of rows affected by the previous query. See:
而不是传递$result
给mysqli_affected_rows
您实际上想要传递数据库链接(由 返回mysqli_connect
),它将为您提供受前一个查询影响的行数。看:
回答by Peter Kiss
echo mysqli_affected_rows($dblink);
The mysqli object contains the count of the affected rows not the result set. I recommend you to use mysqli with OO style or try PDO.
mysqli 对象包含受影响的行数而不是结果集。我建议你使用 OO 风格的 mysqli 或尝试 PDO。
回答by Mohammad Naji
You should only give the $mysqli_link
(in your case $dblink
) , not the $result
in the code that you have written.
您应该只给出$mysqli_link
(在您的情况下$dblink
),而不是$result
您编写的代码中的 。
回答by clement samuel
echo mysqli_num_rows($result);
This should work.
这应该有效。