How to work the return of a query select max?

0

I'll be brief:

When trying to work the values of a query that uses MAX, I get the error:
Use of undefined constant protocol - assumed 'field name'.

Table:

Select:

$atestados=$conect->query("select max(protocolo) from atestados"); 

Php working the return in HTML:

while ( $temp = $atestados->fetch_assoc() ) {
    echo $temp[protocolo];
}

I also tried this:

while ( $temp = $atestados->fetch_assoc() ) {
    echo $temp[max(protocolo)];
}

Error:

Ps. random values for testing

    
asked by anonymous 27.11.2017 / 17:28

2 answers

0

give an alias to max:

$atestados = $conect -> query( "select max(protocolo) AS protocolo from atestados");

and use php like this:

while ( $temp = $atestados->fetch_assoc() ) {
    echo $temp['protocolo'];
} 
    
27.11.2017 / 17:37
0

try using fetch () on for

$atestados = $conect -> query( "select max(protocolo) AS maximo from atestados");
foreach( $temp = $atestados->fetch() ) {
    echo $temp["maximo"];

}

If you do not get results, use the one on your PDO connection

$opcoes = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);
$db = new PDO('pgsql:host=localhost;dbname=test', 'root', '', $opcoes);

and post more details here

    
27.11.2017 / 17:43