简体   繁体   English

如何从 SELECT 查询中获取行数? 错误:mysqli_affected_rows() 期望参数 1 是 mysqli,给定的对象

[英]How to get the number of rows from a SELECT query? Error: mysqli_affected_rows() expects parameter 1 to be mysqli, object given

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?从我在 Google 上搜索到的“对象”是对 OOP 编程方法的引用,但是(据我所知)在这个特定示例中我没有使用 OOP 概念/原则? Or did I misinterpret this error message?还是我误解了这个错误信息?

Thank you.谢谢你。

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.而不是将$result传递给mysqli_affected_rows您实际上想要传递数据库链接(由mysqli_connect返回),这将为您提供受前一个查询影响的行数。 See:看:

http://uk.php.net/mysqli_affected_rows http://uk.php.net/mysqli_affected_rows

echo mysqli_affected_rows($dblink);

The mysqli object contains the count of the affected rows not the result set. mysqli 对象包含受影响的行数而不是结果集。 I recommend you to use mysqli with OO style or try PDO.我建议你使用 OO 风格的 mysqli 或尝试 PDO。

You want to the number of the selected rows.您想要选择的行数。

mysqli::$affected_rows should be used for INSERT, UPDATE, REPLACE or DELETE queries (affected_rows PHP.net) . mysqli::$affected_rows应该用于 INSERT、UPDATE、REPLACE 或 DELETE 查询(affected_rows PHP.net)

For SELECT queries (what you want) there is mysqli_result::$num_rows (num_rows PHP.net) .对于 SELECT 查询(你想要的)有mysqli_result::$num_rows (num_rows PHP.net) As you see num_rows should be used on the result from the SELECT query.如您所见,应该在 SELECT 查询的结果上使用 num_rows。

What you want is echo mysqli_num_rows ( $result ) which gives you the number of the selected rows.您想要的是echo mysqli_num_rows ( $result ) ,它为您提供所选行的数量。

An example for the usage from both of them:他们两个的用法示例:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
echo "Affected rows (INSERT): " . $mysqli->affected_rows; // $mysqli is the link variable to the DB

$result = $mysqli->query("SELECT Code, Name FROM Country ORDER BY Name");
echo "Selected rows: " . $result->num_rows; // Here we use the $result variable from the query

您应该只提供$mysqli_link (在您的情况下$dblink ),而不是您编写的代码中的$result

echo mysqli_num_rows($result);

这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM