Check with IF and INNER JOIN

Asked

Viewed 319 times

0

Guys I have these queries in the database

<?php 
    $consulta = mysql_query("SELECT * FROM mesas LIMIT 50");
    if (mysql_num_rows($consulta)==true) {
       while($lnmesas = mysql_fetch_array($consulta)){ 
       $consultainterna = mysql_query ("SELECT * FROM mesas INNER JOIN pedidos");
?> 

I need to make a comparison of a record in the database in which the column mesas table pedidos is equal to column id table mesas. If the result is positive it gives a echo with a value, otherwise he will give a echo otherwise.

I figured that way it would work:

if($lnmesas['pedidos.mesas'] == $lnmesas['mesas.id']){
    echo "success":
}else{
    echo "danger";
}

But unfortunately it didn’t work. Can anyone explain to me the correct way to do this, I’m beginner, I don’t have much experience yet.

here is the structure of my tables... tabela pedidos

tabela mesas

  • Which table is which ai rsss?

2 answers

2


Try using MYSQL on, below is an example:

SELECT * FROM mesas m LEFT JOIN pedidos p ON p.mesas = m.id

Then just check with PHP

<?php

     $consulta = mysql_query("SELECT * FROM mesas m LEFT JOIN pedidos p ON p.mesas = m.id LIMIT 50") or die (mysql_error());

     while($dados = mysql_fetch_assoc($consulta)) {
          if(!empty($dados['p.id'])) {
              echo "success";
          } else {
              echo "danger";
          }
     }

Also consider studying PDO USE, MySQL_* is already an obsolete php function.

  • I’m with the two projects, this one, and the other study mysqli_*, thanks for the tip and the answer.

  • Warning: mysql_fetch_array() expects Parameter 1 to be Resource, Boolean Given in

  • @Alfredolima is some column in the query that is problematic, copies it and runs in your PHP My Admin or Workbench. Ai picks up the error and passes me here.

  • 1

    I added an error handling. in the code.

0

Hello, you should post the schema of your database.

But come on. Supposing in your database pedidos.mesas and mesas.id are the same kind, so we’d do:

SELECT IF(
          /*Condição, se não for NULL é porque há pedido*/
          NOT ISNULL(p.mesas),

          /*Se verdadeiro retornará 'success'*/
          'success',

          /*Se falso retornará 'danger'*/
          'danger'
         ) as retorno_coluna,
  m.*, /*Também retorna as colunas da tabela mesas*/
  p.*  /*Também retorna as colunas da tabela pedidos*/
FROM mesas m
/*Left Join, visto que você quer recuperar
  a lista de mesas independente do pedido*/
LEFT JOIN pedidos p ON m.id = p.mesas

Well the above query should work. But if there are columns with the same names in both tables, you, instead of returning p.* and m.*, you must return by name each column you will need. See the result in phpMyAdmin: phpMyAdmin

So combining everything with PHP is like this:

    <?php
        $consulta = mysql_query("SELECT IF(NOT ISNULL(p.mesas), 'success', 'danger') as retorno_coluna, m.*, p.* FROM mesas m LEFT JOIN pedidos p ON m.id = p.mesas");
        if (mysql_num_rows($consulta)==true)
          {
            while($lnmesas = mysql_fetch_array($consulta))
              {?>         
        <div class="col-sm-4 col-md-2">
          <div class="ls-box">
          <div class="ls-alert-<?php echo $lnmesas['retorno_coluna']; ?>">
            <h6 class="ls-title-1"><?php  echo $lnmesas['retorno_coluna']; ?></h6>
              <strong><?php echo $lnmesas['id']  ?></strong>
          </div>
        <a href="index.php?mod=info-mesa&mesaid=<?php echo $lnmesas['id'] ?>">
          <button class="ls-btn-primary ls-ico-cog">Gerenciar Mesa</button><a>  
          </div>
        </div><?php
               }
          }
        else
          {?>
       <center>
       <div class="ls-alert-warning"><strong>OPS! </strong> Não existe nenhuma mesa com pedidos, cheque se você realmente lançou seu pedido clicando na aba pedidos aqui do lado</div>
       </center><?php
          }
    ?> 
  • syntax error, Unexpected 'IF' (T_IF) in .... on line 23 23>> <?php SELECT IF( it returned this error

  • I updated the question and put the structure of my tables to see if it helps you with the answer, thank you so much des.

  • @Alfredolima, put your PHP code, because I tested the query above and it worked on phpMyAdmin.

  • I think I understand what happened. Do the following @Alfredolima put the Query inside $query = mysql_query("INSIDE");

  • look my code is this here http://pastebin.com/KyGv0J8Z on line 9 and on line 10 is where I need to do the if

  • As I understood in your code there is the Danger value in your db and mine does not exist, I will give another example...... I want to change the class of a div example: <div class'ls-Alert-<? php inside if that displays Danger or Success ? >"> </div>

  • Not @Alfredolima, there is no Danger value. I based myself on your initial code where you printa Success or Danger depending on the result. Then the query does the test and already brings the result inside the column 'return_column'. If there’s something you didn’t understand in the PHP code above and in the MYSQL query explain your question there.

  • is because I made the code exactly the same as yours, I even copied the code to see if I had typed something wrong and yet it prints the same error Warning: mysql_num_rows() expects Parameter 1 to be Resource, Boolean Given in C: xamp htdocs Projects Moclient 2.0 modulos tables tables.php on line 17 and on line 17 it has this code if (mysql_num_rows($query)=true)

Show 3 more comments

Browser other questions tagged

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