热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MySQLi查询在表中获取最大id?-MySQLiquerytogetmaximalidinthetable?

$mysqlinewmysqli(localhost,root,,mydatabase);if($result$mysqli->prepare(SELE
 $mysqli = new mysqli("localhost","root","","mydatabase");

 if ($result = $mysqli->prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
 {

    $id = 2;
    $result->bind_param("ii",$id,$id);
    $result->execute();
    $result->bind_result($max);
    $result->close();

    var_dump($max);

 }

 $mysqli->close();

Unfortunately this code always showing NULL, can u folk explain me how to reach a result?

不幸的是,这段代码一直都是空的,你能告诉我怎么去实现吗?

updated:

更新:

in console mode staff like this works great. field id is int and incremental (as PRIMARY INDEX), other fields it's just a rows with a different int values, I cant change anything.

像这样的控制台模式工作人员非常出色。字段id是int和增量(作为主索引),其他字段只是具有不同int值的行,我不能更改任何东西。

updated:

更新:

Well, seems I found the solution:

看来我找到了解决方案:

 $mysqli = new mysqli("localhost","root","","mydatabase");

 if ($result = $mysqli->prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
 {

    $id = 2;
    $result->bind_param("ii",$id,$id);
    $result->execute();
    $obj = $result->get_result()->fetch_object();
    $max = $obj->id;
    $result->close();

    var_dump($max);

 }

 $mysqli->close();

this is it.

这是它。

2 个解决方案

#1


2  

You still need to call fetch, as max will only be available after that point. See doc: http://php.net/manual/en/mysqli-stmt.bind-result.php

您仍然需要调用fetch,因为max只在那之后才可用。看到医生:http://php.net/manual/en/mysqli-stmt.bind-result.php

$result->bind_result($max);

/* fetch values */
while ($result->fetch()) {
    printf("Max ID %i\n", $max);
}

$result->close();

#2


2  

I figured it out this way:

我是这样想的:

$result = mysqli_query($con, "SELECT * FROM `TableName` ORDER BY `PID` DESC LIMIT 1");
$row = mysqli_fetch_array($result);
$pidmax=$row['PID'];

推荐阅读
author-avatar
负能量包子玻璃包各负磁场宣泄区
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有