Er Vaibhav Vatsa
Er Vaibhav Vatsa

Reputation: 9

i want the largest number from array in php

$select=$conn->query("SELECT `id` FROM `order` where `customer`='$id'");
while ($result=$select->fetch_assoc()) { 
    echo $result['id'];                                                                                                    
}

I got the two values.

How to print the large number ? php max function is not working for me

Upvotes: 0

Views: 95

Answers (4)

dipmala
dipmala

Reputation: 2011

You can directly do in query check below query for the same.

$select=$conn->query("SELECT id FROM order where customer='$id' order by id desc limit 1");

Upvotes: 0

Abu Bin Oalid
Abu Bin Oalid

Reputation: 31

In your way

$select=$conn->query("SELECT `id` FROM `order` where `customer`='$id'");
$maxVal = 0;
while ($result=$select->fetch_assoc()) { 
    if($maxVal<$result['id']){
        $maxVal=$result['id'];
    }                                                                                                    
}
echo $maxVal;

But better is

$select=$conn->query("SELECT max(id) FROM order where customer='$id'");
$result=$select->fetch_assoc();
echo $result['id'];

Or if your id is auto-increment then you can use

$select=$conn->query("SELECT id FROM order where customer='$id' order by id desc Limit 1");
$result=$select->fetch_assoc();
echo $result['id'];

Upvotes: 0

RAUSHAN KUMAR
RAUSHAN KUMAR

Reputation: 6006

you can also take the max of id using sql as

SELECT max(id) FROM order where customer='$id'

Upvotes: 1

user10226920
user10226920

Reputation:

faster to do it in the query:

SELECT id FROM order where customer='$id' order by id desc Limit 1

Upvotes: 3

Related Questions