添加mysql结果时,我无法获得总数。
<?php
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT 1 ");
while( $row = mysql_fetch_assoc( $result ) ){
echo "{{$row['id']}+1}";
}
?>
这里我得到的结果为&#34; 27 + 1&#34;。
但我想&#34; 28&#34;。请指出我出错的地方。
答案 0 :(得分:3)
PHP认为您正在尝试连接字符串,因此我建议您明确添加两个数字并回显结果:
<?php
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT 1 ");
while( $row = mysql_fetch_assoc( $result )) {
$incremented = $row['id'] + 1;
echo $incremented;
}
?>
此外,与您的问题没有直接关系,我建议您停止使用mysql_query
和类似的功能,因为它们已被弃用,而在PHP 7中则会被删除。请尝试使用PDO或mysqli扩展。
答案 1 :(得分:0)
您的要求将使用预增量运算符进行全填充:
<?php
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table ORDER BY ID DESC LIMIT 1 ");
while( $row = mysql_fetch_assoc( $result )) {
echo ++$row['id'];
}
?>
请参阅Result
答案 2 :(得分:0)
你也可以通过查询直接完成它(如果你想继续添加id)
SELECT count(id) as idCount FROM table ORDER BY ID DESC LIMIT 1
如果你想知道总数
SELECT count(*) as idCount FROM table ORDER BY ID DESC LIMIT 1
并在应用层上
echo $row['id']+1;
将完成这项工作。
答案 3 :(得分:-1)
为什么你不能在查询本身中解决这个问题,试试这个..
$result = mysql_query("SELECT (id+1) as id, col2 FROM table ORDER BY ID DESC LIMIT 1 ");