PHP MySQL max()函数产生5 [英] PHP MySQL max() function producing 5

查看:95
本文介绍了PHP MySQL max()函数产生5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究由PHP和MySQL创建的调查.所以问题是,我正在尝试创建一个ResponseID,这是每个提交调查的人都特定的ID.因此,创建代码的目的是将ResponseID列中的现有最大值加1.这是代码:

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; 

我知道我可以压缩它,但这是问题所在:我已经在表上输入了一个ResponseID为1的项目.当我用不同的答案测试调查时,下一个ResponseID为5.应该是2.因此,我再次进行测试,以查看下一次是否会产生6个结果.

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.

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

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.

我对PHP有点陌生.

I am somewhat new to PHP.

推荐答案

$ res将是mysql语句句柄,而不是查询结果.您仍然必须实际从$ res结果中获取一行,以访问查询中的max()函数的值.

$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.

此句柄可能在内部具有标识符#5,这就是为什么您得到奇数"结果的原因.

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

代码应为:

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

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

这篇关于PHP MySQL max()函数产生5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆