How to display the id as form counter [closed]

-2

I'm creating a form counter and would like to know if I'm going the right way?

This is my select query

<?php
error_reporting(-1);
ini_set('display_errors', 'On');

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
     echo"Nossas falhas local experiência ..";
     exit();
}



         $sql= "SELECT MAX(m.id_pedido)
                FROM mpedido as m WHERE id_pedido
                ORDER BY m.id_pedido DESC
                LIMIT 1";
        $resultado_id= mysqli_query($link,$sql);

?>

and this is the php code that displays the id as counter. but type is giving an error

  

Notice: Array to string conversion in   C: \ xampp \ htdocs \ Projects \ order.php on line 61

  <span>Comanda:</span>
                    <?php


                while($row = mysqli_fetch_array($resultado_id)){
                echo '<span>'.["id_pedido"].'</span>';

                }

                ?>

How could I make a counter from the id

    
asked by anonymous 21.01.2017 / 17:07

2 answers

3

The error is in:

echo '<span>'.["id_pedido"].'</span>';

This does not exist.

When using the code:

while($row = mysqli_fetch_array($resultado_id)){

}

You are saying that as long as there are rows, you will get the array $row , or should be used:

while($row = mysqli_fetch_array($resultado_id)){

 echo '<span>'.$row["id_pedido"].'</span>';

}

But ... This will not work either. The reason is that there is no index id_pedido , and the reason for not existing is because there is no such thing defined in SELECT .

  

You do not need to use while() if there is LIMIT 1 , if the limit is 1 it will run only once.

Possible fixes:

Method 0. Use numeric index:

While While:

$row = mysqli_fetch_array($resultado_id);
echo '<span>'.$row[0].'</span>';

Method 1. Name the steers:

Part of the Query:

$sql= "SELECT MAX(m.id_pedido) as ultimo FROM mpedido as m WHERE id_pedido ORDER BY m.id_pedido DESC LIMIT 1";

$resultado_id= mysqli_query($link,$sql);

So now SELECT has a result with the name ultimo for the result of MAX(m.id_pedido) , which is easier.

While While:

$row = mysqli_fetch_array($resultado_id);
echo '<span>'.$row['ultimo'].'</span>';
    
22.01.2017 / 01:07
2

I know if I understand correctly, but I believe you want something like:     

//Criar a conexao
$link = new mysqli ("localhost", "root", "", "peixaria");
if($link->connect_errno){
 echo"Nossas falhas local experiência ..";
 exit();
}   

$sql= "SELECT MAX(m.id_pedido) as ultimo_id FROM mpedido as m";
$resultado_id= mysqli_query($link,$sql);
?>

Since you have used MAX (), it will return the largest value in the order_id field, so if it is a sequential field it will return the desired value.

Now in php you should do so

<span>Comanda:</span>
<?php
  while($row = mysqli_fetch_array($resultado_id)){
   echo '<span>'.$row["ultimo_id"].'</span>';
  }

? >

When you do this while, you receive a line with the information returned, but it comes as an array, so you need to access the index you created in SQL, in the case "ultimo_id".

I hope this is what you are looking for.

    
21.01.2017 / 22:24