Logic in MySQL Query only if 2 conditions are met

1

I have the following code:

$query = "SELECT * FROM pedidos  WHERE usuario = '$usua' AND status_pedido = 'ESPERANDO' ";
        $result = mysqli_query($db, $query);
if ($result){
    echo '<div class="alert alert-danger" role="alert"" >
                <h3>
        LO SENTIMOS, USTED POSEE UN PEDIDO EN ESPERA        
                </h3>
            </div>';


} else { echo '<div class="alert alert-danger" role="alert"" >
                <h3>
        AQUI SE IMPRIME UN FORMULARIO       
                </h3>
            </div>';}

The idea is that if condition 1 and condition 2 of results are met

The usuario = '$usua' condition will almost always show results but not necessarily the condition status_pedido = 'ESPERANDO' as it always gives results even when the second condition is "ENTREGADO"

I would like to know if there is a way to change the way to query MySQL.

    
asked by Jose M Herrera V 30.08.2018 в 15:17
source

1 answer

3

What happens is that mysqli_query returns a True or a False as long as the query executes successfully, that is to say only will send a False when there is an error, what you need is to know if the number of rows is greater than zero, that is if there are records with the conditions you require for it you should use something like this:

$query = "SELECT * FROM pedidos  WHERE usuario = '$usua' AND status_pedido = 'ESPERANDO' ";
        $result = mysqli_query($db, $query);
        $rows =  mysqli_num_rows($result)
if ($rows > 0){
    echo '<div class="alert alert-danger" role="alert"" >
                <h3>
        LO SENTIMOS, USTED POSEE UN PEDIDO EN ESPERA        
                </h3>
            </div>';


} else { echo '<div class="alert alert-danger" role="alert"" >
                <h3>
        AQUI SE IMPRIME UN FORMULARIO       
                </h3>
            </div>';}

Documentation: link

    
answered by 30.08.2018 / 16:48
source