How to work the return of a select max query?

Asked

Viewed 84 times

0

I will 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: Tabela atestados

Select:

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

Php working the return in HTML:

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

also tried so:

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

Error: erro

Ps. Random values for testing

  • You are using PDO or mysqli?

2 answers

0

give an alias for max:

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

and use php that way:

while ( $temp = $atestados->fetch_assoc() ) {
    echo $temp['protocolo'];
} 
  • Didn’t help...

  • What mistake now?

0

try using fetch() no for

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

}

if no results, use 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.