Reputation: 303
$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.
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.
Upvotes: 2
Views: 11423
Reputation: 147
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'];
Upvotes: 6
Reputation: 14237
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
$result->bind_result($max);
/* fetch values */
while ($result->fetch()) {
printf("Max ID %i\n", $max);
}
$result->close();
Upvotes: 2