在不解析数组的情况下递增整数

时间:2015-01-22 22:43:29

标签: php mysql

我有一个提交给MySQL的php表单,以及第二个使用第一个存储的值的php表单。

数据库中booth_member的最高值目前为3.在下面的示例中,= $ booth_member应该等于4,但总是返回2.

$membersql = $mysqli->query("SELECT booth_member FROM users WHERE booth_number = '{$user['booth_number']}' ORDER BY booth_member DESC LIMIT 1");
$booth_member = $membersql + 1;
echo $booth_member;

如果我尝试回显$ membersql,它只返回'数组。'

3 个答案:

答案 0 :(得分:1)

mysqli::query会返回一个资源对象,这就是它无法正常工作的原因。一些代码来解决它:

$membersql = $mysqli->query("SELECT booth_member FROM users WHERE booth_number = '{$user['booth_number']}' ORDER BY booth_member DESC LIMIT 1");
if($membersql->num_rows < 1)
{ /* error handling goes here */ }
else
{
  $res_array = $membersql->fetch_assoc(); // save result as array
  $booth_member = $res_array["booth_member"] + 1;
  echo $booth_member;
}

请务必先转义$user['booth_number']以防止sql注入。

有关详细信息,请查看mysqli::query文档:)

答案 1 :(得分:0)

对于生产系统,您需要一堆try / catch代码,但要测试:

$result = $mysqli->query("SELECT booth_member FROM users WHERE booth_number = '{$user['booth_number']}' ORDER BY booth_member DESC LIMIT 1");

$booth_member = $result->fetch_object('booth_member');
++$booth_member;
echo $booth_member;

答案 2 :(得分:-2)

那是因为它是一个数组。我相信你是想做的:

$booth_member = $membersql['booth_member'] + 1;