Josh
Josh

Reputation: 133

What's wrong with this mysqli query?

I'm trying to figure out what's wrong with my mysqli code below. The data is definitely in the table. I have 2 questions:

  1. Why isn't this giving me any results?
  2. Could I have the echo <table></table> section in a separate php file, if yes, how would I call $name,$partner,$cell etc.?

      <?php
    
      $mysqli = new mysqli($host, $uname, $pword, $db);
    if ($mysqli->connect_errno)
    {
    echo "Connection failed: %s\n", $mysqli->connect_error;
    exit();
    }
    
      $query = ("SELECT * FROM contacts WHERE contacttype IN ('Buyer','Seller','Buyer / Seller','Investor') AND leadstatus = 'New' ORDER BY date DESC");
    
      $result = $mysqli->query($query);
    
      while ($row = $result->fetch_array()) {
    
      $space = (!empty($row['firstname']) && !empty($row['lastname'])) ? ' ' : '';        
      $name = $row['firstname'].$space.$row['lastname'];       
      $partner  = $row['spousefirst'];   
      $cell = (!empty($row['phonecell'])) ? " {$row['phonecell']} (cell)" : ''; 
      $email = (!empty($row['email'])) ? " {$row['email']} (email)" : '';
      $ID = $row['ID'];
    
      echo'<table>
      <tbody>
      <tr>
        <td><input type="checkbox" name="" id="" value="<? echo $ID; ?>"></td>
        <td><a href="/backend/leads/view/?ID=<? echo $ID; ?> "><strong><? echo $name; ?></strong></a></td>
        <td><a href="/backend/leads/view/?ID=<? echo $ID; ?> "><? echo $partner; ?></a></td>
        <td><? echo $phonecell; ?></td>
        <td><a href="mailto:<? echo $email; ?>"><? echo $email; ?></a></td>
        <td><? echo date("M jS, g:i A", strtotime($date)); ?></td>
        <td><? echo $contacttype; ?></td>
        <td><? echo $agentassigned; ?></td>
        <td><? echo $leadstatus; ?></td>
        <td><a href="/backend/contacts/notes.php?ID=<? echo $ID; ?>">View </a>+ </td>
        <td><a href="/backend/contacts/todo.php?ID=<? echo $ID; ?>">View </a>+ </td>
        <td><a href="/backend/contacts/deletesuccess.php?ID=<? echo $ID; ?>">D</a></td>
        </tr>
        </tbody>
    </table>';
         }
         ?>
    

EDIT:

It's showing the <table> but just not giving me values for $name, $email...ect.

Upvotes: 0

Views: 340

Answers (2)

Ander2
Ander2

Reputation: 5658

The final echo does not look well constructed. I would prefer to use a concatenated string to output the variables, just like this( I put only a few lines as example):

echo'<table>
<tbody>
   <tr>
     <td><input type="checkbox" name="" id="" value="'. $ID .'"></td>
     <td><a href="/backend/leads/view/?ID='. $ID .'"><strong>'. $name.' </strong></a></td> 

Upvotes: 2

BOMEz
BOMEz

Reputation: 1010

The query looks solid as does the rest of your code. Here are a few troubleshooting steps to try out:

  1. echo out $query and run it directly against your database. Do you get results? If you don't get any results from a direct query then that means the query is returning 0 rows and your tables will end up empty as well.
  2. Do a var_dump($result) to verify there is information being stored in there. You could also add change $result = $mysqli->query($query); to $result = $mysqli->query($query) or die $mysqli->error;

The results from these 2 steps should give you a good idea on where things are going wrong. In general you want to stop through each part of the query process and look at every variable involved to ensure that it has the data you expect stored inside of it.

To answer your 2nd question: yes you can. If you put the code for the table in a separate file and then include()it, the variables will still be available to the included code.

See the PHP manual on include()

Upvotes: 2

Related Questions