PHP MySQL max() 函数产生 5

发布于 2024-12-05 18:42:24 字数 523 浏览 1 评论 0原文

我正在开展一项由 PHP 和 MySQL 创建的调查。所以问题是,我正在尝试创建一个 ResponseID,它是每个提交调查的人特定的 ID。因此,创建了代码来将 ResponseID 列中的现有最大值加 1。代码如下:

//Response ID creation 
$query = "SELECT max(ResponseID) FROM survey";
$res = mysql_query($query);
$RID = $res+1; 

我知道我可以压缩它,但问题是:我已经在表格中输入了一项,ResponseID 为 1。当我用不同的答案测试调查时,下一个 ResponseID 是 5。它应该是 2于是我又测试了一下,看看下次会不会产生6。

不幸的是,它又产生了5。我让我的 PHP 专家检查了一下,他说编码是正确的,应该是来自数据库的东西。我没有在 ResponseID 中设置任何内容,除了它是一个 int 之外。那么为什么它会产生 5 呢?如果有人能告诉我如何修复它,那真是太酷了。

我对 PHP 有点陌生。

I am working on a survey created by PHP and MySQL. So the issue is, I am trying to create a ResponseID, which is an ID specific for every person submitting the survey. So the code was created to add 1 to the existing max value from the ResponseID column. Here's the code:

//Response ID creation 
$query = "SELECT max(ResponseID) FROM survey";
$res = mysql_query($query);
$RID = $res+1; 

I know I can condense it, but here's the problem: I already entered one item on the table with the ResponseID of 1. When I tested the survey with different answers, the next ResponseID was 5. It should have been 2. So I tested again to see if it would produce 6 next time.

Unfortunately, it produced 5 again. I had my PHP guru looked it over and he said the coding was correct and it should be something from the database. I didn't set anything in the ResponseID except for it being an int. So why is it producing a 5? If anyone could please tell me how to go about fixing it, that would be super cool of you.

I am somewhat new to PHP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

—━☆沉默づ 2024-12-12 18:42:24

$res 将是 mysql 语句句柄,而不是查询的结果。您仍然需要从 $res 结果中实际 FETCH 一行来访问查询中 max() 函数的值。

该句柄可能在内部具有标识符 #5,这就是为什么您会得到“奇怪”结果的原因。

代码应该是:

$sql = "SELECT MAX(responseID) AS max ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$RID = $row['max'] + 1;

$res will be a mysql statement handle, NOT the result of the query. you still have to actually FETCH a row from this $res result to access the value of the max() function in the query.

This handle probably internally has identifier #5, which is why you're getting that "odd" result.

The code should be:

$sql = "SELECT MAX(responseID) AS max ...";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);

$RID = $row['max'] + 1;
放肆 2024-12-12 18:42:24

$res是资源,不是查询的值,请阅读手册: http://nz.php.net/manual/en/function.mysql-query.php

$res is a resource, not the value of the query, please read the manual: http://nz.php.net/manual/en/function.mysql-query.php

北斗星光 2024-12-12 18:42:24

为什么不使用 AUTO_INCRMENT 列并使用 mysql_insert_id 检索新值?

Why don't you use an AUTO_INCREMENT column and retrieve the new value using mysql_insert_id?

风筝在阴天搁浅。 2024-12-12 18:42:24

为此,您需要使用 AUTO_INCRMENT 列。问题是,如果 PHP 脚本的两个实例同时运行(极有可能假设您使用的是 Apache 等多线程服务器),那么您可能会遇到这样的情况:(

|PHP #1         |  PHP #2       |
=================================
|Accept client  |(waiting)      |
|SELECT MAX...  |(waiting)      |
|Send to MySQL  | Accept client |
|(waiting)      | SELECT MAX... |
|(waiting)      | Send to MySQL |
|Get response 4 |Get response 4 | //Nothing inserted yet, max is still 4
|Try to insert 5| (waiting)     |
|Succeeded      | (waiting)     |
|(waiting)      |Try to insert 5|
| ...           | Failed!       |

除了 Dagon 所说的之外)

You need to use an AUTO_INCREMENT column for this. The problem is that if two instances of the PHP script are running at the same time (extremely likely assuming you're using a multithreaded server like Apache), then you could have a case like this:

|PHP #1         |  PHP #2       |
=================================
|Accept client  |(waiting)      |
|SELECT MAX...  |(waiting)      |
|Send to MySQL  | Accept client |
|(waiting)      | SELECT MAX... |
|(waiting)      | Send to MySQL |
|Get response 4 |Get response 4 | //Nothing inserted yet, max is still 4
|Try to insert 5| (waiting)     |
|Succeeded      | (waiting)     |
|(waiting)      |Try to insert 5|
| ...           | Failed!       |

(This in addition to what Dagon said)

想挽留 2024-12-12 18:42:24

使用 $row = mysql_fetch_assoc($res) 从查询中获取结果行。

请参阅 mysql_fetch_assoc()

Use $row = mysql_fetch_assoc($res) to get the resulting row from your query.

See mysql_fetch_assoc().

靖瑶 2024-12-12 18:42:24

您没有获得正确值的原因是因为 mysql_query 不返回值,它返回资源。

试试这个:

//Response ID creation 
$query = "SELECT max(ResponseID) FROM survey";
$res = mysql_query($query);
if($res !== FALSE) $res = mysql_fetch_array($res);   // If $res === FALSE, something went wrong
$RID = $res[0] + 1;

另外我建议你在 ResponseID 字段上使用 AUTO_INCRMENT,这会让你的生活变得更轻松:)

The reason why you are not getting the correct value is because mysql_query doesn't return a value, it returns a resource.

Try this instead:

//Response ID creation 
$query = "SELECT max(ResponseID) FROM survey";
$res = mysql_query($query);
if($res !== FALSE) $res = mysql_fetch_array($res);   // If $res === FALSE, something went wrong
$RID = $res[0] + 1;

Also I suggest you to use AUTO_INCREMENT on the ResponseID field, this makes your life a lot easier :)

冷默言语 2024-12-12 18:42:24

我不确定我是否理解您的问题,但如果您想生成特定于每个用户或发布的每个调查的响应 ID,那么您可以做的是使用 auto_increment 作为响应 ID。每次发布新调查时,响应 ID 都会增加。还可以使用 mysql_insert_id 获取最后发布的 id 来获取最后发布的 id。

获取最后一个ID

$last_id = mysql_insert_id ();

所以你可以使用你需要将该语句放在你的查询之后来 。这样你就可以得到最后的id。

i'm not really sure if i understood ur problem but if u wanna generate a response ID specific to every user or every survey posted, then what u can do is use auto_increment for the response ID. the response id will be incremented every time a new survey is posted. also get the last id posted using mysql_insert_id to get the last id posted.

so u can get the last id using

$last_id = mysql_insert_id ();

u need to put that statement right after ur query. that way u can get the last id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文