PHP MySQL max() 函数产生 5
我正在开展一项由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
$res 将是 mysql 语句句柄,而不是查询的结果。您仍然需要从 $res 结果中实际 FETCH 一行来访问查询中 max() 函数的值。
该句柄可能在内部具有标识符 #5,这就是为什么您会得到“奇怪”结果的原因。
代码应该是:
$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:
$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
为什么不使用
AUTO_INCRMENT
列并使用mysql_insert_id
检索新值?Why don't you use an
AUTO_INCREMENT
column and retrieve the new value usingmysql_insert_id
?为此,您需要使用
AUTO_INCRMENT
列。问题是,如果 PHP 脚本的两个实例同时运行(极有可能假设您使用的是 Apache 等多线程服务器),那么您可能会遇到这样的情况:(除了 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:(This in addition to what Dagon said)
使用
$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().
您没有获得正确值的原因是因为 mysql_query 不返回值,它返回资源。
试试这个:
另外我建议你在 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:
Also I suggest you to use AUTO_INCREMENT on the ResponseID field, this makes your life a lot easier :)
我不确定我是否理解您的问题,但如果您想生成特定于每个用户或发布的每个调查的响应 ID,那么您可以做的是使用 auto_increment 作为响应 ID。每次发布新调查时,响应 ID 都会增加。还可以使用 mysql_insert_id 获取最后发布的 id 来获取最后发布的 id。
获取最后一个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
u need to put that statement right after ur query. that way u can get the last id.