Obtain the maximum value of a database

0

Good afternoon, I'm trying to capture a value from my database through a query but this gives me an error, the code is as follows:

    $consultarRepresentante = mysqli_query($conexion, 
    "select max(id_representante) from representantes")  or die 
    ("Problemas en el query".mysqli_error($conexion));
    $registros = mysqli_fetch_array($consultarRepresentante);
    echo $registros;

Here is the error:

    
asked by shadowmors 19.11.2016 в 19:13
source

4 answers

5

Keep in mind that $registros refers to all the columns that you extract from a table, if you want to refer to a single column you should do it like this:

echo $registros['id_representante'];
    
answered by 19.11.2016 / 19:19
source
0

$ records is the arrangement with the result of the query I suggest you do:

 $consultarRepresentante = mysqli_query($conexion, 
    "select max(id_representante) as max_id from representantes")  or die 
    ("Problemas en el query".mysqli_error($conexion));
$registros = mysqli_fetch_array($consultarRepresentante);
echo $registros['max_id'];
    
answered by 20.11.2016 в 20:12
0

I would do it this way:

SELECT id FROM representantes ORDER BY id DESC LIMIT 1
    
answered by 21.11.2016 в 17:03
0

mysqli_fetch_array: tells you the answer format of your query. There are others such as object, assoc. More info on this link: link

    
answered by 21.11.2016 в 19:01