简体   繁体   English

PHP / mysqli:带有num_rows的预处理语句不断不返回任何内容

[英]PHP / mysqli: Prepared Statements with num_rows constantly returning nothing

In my test-surroundings there is a database containing some Person Information (Name, E-Mail, Adress etc.). 在我的测试环境中,有一个包含一些人员信息(姓名,电子邮件,地址等)的数据库。 These Informations can be inserted by anyone into the database via a form. 任何人都可以通过表格将这些信息插入数据库。 In the background they are inserted with a parameterized INSERT into the database after submission. 提交后,它们在后台用参数化的INSERT插入数据库中。
What I now would like to do is to detect if some person tries to insert the same values into the database again, and if he does, not inserting the new values and instead showing an error message. 我现在想做的是检测是否有人尝试再次将相同的值插入数据库,如果有人这样做,则不插入新值,而是显示错误消息。 (So every person name in the database is unique, there are no multiple rows linked to one name). (因此,数据库中的每个人的姓名都是唯一的,没有多个行链接到一个姓名)。
I had a numerous number of ideas on how to accomplish this. 关于如何实现这一点,我有很多想法。 My first one was to use a query like REPLACE or INSERT IGNORE , but this method would not give me feedback so I can display the error message. 我的第一个方法是使用REPLACEINSERT IGNORE类的查询,但是此方法不会给我反馈,因此我可以显示错误消息。
My second attempt was to first do a SELECT -query, checking if the row already exists, and if num_rows is greater than 0, exit with the error message (and else do the INSERT -part). 我的第二个尝试是首先执行SELECT -query,检查该行是否已存在,并且num_rows大于0, exit并显示错误消息(否则执行INSERT -part)。 For this to work I will have to use parameterized queries for the SELECT too, as I´m putting some user input into it. 为此,我还要对SELECT使用参数化查询,因为我正在向其中输入一些用户输入。 Figuring that parameterized queries need special functions for everything you could normally do with way less lines of code, I researched in the internet on how to get num_rows from my $statement parameterized-statement-object. 考虑到参数化查询需要特殊功能来完成通常可以用更少的代码行完成的所有事情,我在互联网上研究了如何从$ statement parameterized-statement-object中获取num_rows This is what I had in the end: 这是我最后得到的:

$connection = new mysqli('x', 'x', 'x', 'x');
if (mysqli_connect_error()) {
    die("Connect Error");
}
$connection->set_charset("UTF-8");
$statement = $connection->stmt_init();
$statement = $connection->prepare('SELECT Name FROM test WHERE Name LIKE ?');
flags = "s";
$statement->bind_param($flags, $_POST["person_name"]);
$statement->execute();
$statement->store_result();
$result = $statement->get_result(); //Produces error
if ($result->num_rows >= 1) {
    $output = "Your already registered";
} else {
    $output = "Registering you...";
}
exit($output);

After all, I can´t get why mysqli still won´t give me num_rows from my statement. 毕竟,我不明白为什么mysqli仍然不会从我的陈述中给我num_rows Any help is appreciated, thanks in advance! 任何帮助表示赞赏,在此先感谢!
Oh, and if you guys could explain to me what I have to do to get affected_rows ,that would be awesome! 哦,如果你们可以向我解释要获得affected_rows我必须做什么,那就太好了!

EDIT: I know I could to this by using unique constraints. 编辑:我知道我可以通过使用唯一的约束。 I also found out that I can find out if INSERT IGNORE skipped the INSERT or not. 我还发现,我可以确定INSERT IGNORE跳过了INSERT But that won´t answer my complete question: Why does the SELECT num_rows alternative not work? 但这无法回答我的完整问题:为什么SELECT num_rows替代方法不起作用?

ANOTHER EDIT: I changed the code snippet to what I now have. 另一个编辑:我将代码片段更改为现在的内容。 Although my mysql(i)-version seems to be 5.6.33 (I echo´d it via $connection->server_info ) get_result() produces the following error message: 尽管我的mysql(i)版本似乎是5.6.33 (我通过$connection->server_info )get_result()产生以下错误消息:
Fatal error: Call to undefined method mysqli_stmt::get_result() in X on line X (line of get_result ) 致命错误:在X行( get_result行)的X中调用未定义的方法mysqli_stmt :: get_result()

The behaviour of mysqli_num_rows() depends on whether buffered or unbuffered result sets are being used. mysqli_num_rows()的行为取决于是否使用缓冲的或未缓冲的结果集。 For unbuffered result sets, mysqli_num_rows() will not return the correct number of rows until all the rows in the result have been retrieved. 对于无缓冲的结果集,在检索到结果中的所有之前, mysqli_num_rows() 不会返回正确的行数 Note that if the number of rows is greater than PHP_INT_MAX , the number will be returned as a string. 请注意,如果行数大于PHP_INT_MAX ,则该数字将作为字符串返回。

Also make sure that you declare ->store_result() first. 另外,请确保先声明->store_result() Moreover the function doesn't work with LIMIT used jointly with SQL_CALC_FOUND_ROWS . 此外,该功能不适用于与SQL_CALC_FOUND_ROWS一起使用的LIMIT If you want to obtain the total rows found you must do it manually. 如果要获取找到的总行数,则必须手动执行。

EDIT: 编辑:

If nothing from the suggestions does not work for you, then I would propose to rewrite your SQL query: 如果建议中的任何内容都不适合您,那么我建议重写您的SQL查询:

 SELECT `Name`, (SELECT COUNT(*) FROM `Persons`) AS `num_rows` FROM `Persons` WHERE `Name` LIKE ?

This query will return the total number from your Persons table, as well as Name , if exist. 该查询将返回您的“ Persons表中的总数以及Name (如果存在)。

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

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