Problem showing result in php when there is only one entry in the BD

Asked

Viewed 61 times

0

The code below returns some value, when there are several records with the same or similar name, when there is only one record in the bd does not return the value of that record.

<?php
include_once '../../Modulos/Database/Banco.php';

$pesquisa=$_POST['pesquisa'];
echo ($pesquisa);
$sql = "select * from fornecedor where razaoSocial like '%$pesquisa%' or nomeFantasia like '%$pesquisa%';";
$salva = mysqli_query($conexao, $sql);

if ($resultado = mysqli_fetch_array($salva)) {
   while ($resultado = mysqli_fetch_array($salva)) {

       $id = $resultado[0];
       $razaoSocial = $resultado[1];
       $cnpj = $resultado[2];
       $nomeFantasia = $resultado[3];
       $tel = $resultado[4];
       $contato = $resultado[5];
       $email = $resultado[6];
       //$dataCadastro = $resultado[6]
       // testando a função edita.php
       //começará com 0 e vai evoluindo, isso também pode registrar quantas resultados tem a tabela
       $parimpar++;
       //A tabela do sbadmin precisa marcar com par ou ímpar cada linha
       if ($id % 2 == 0) {
           $parimpar = "odd";
       } else {
           $parimpar = "even";
       }

       //echo"<tr><th>$id</th><th>$nome</th><th>$codBarra</th><th>$descricao</th><th>$vCusto</th><th>$vVenda</th></tr>";

       echo'

       <tr class="' . $parimpar . ' gradeX">
       <td><a href="cadastrarPedido.php?id=' . $id . '">' . $id . '</a></td>
       <td>' . $razaoSocial . '</td>
       <td>' . $cnpj . '</td>
       <td>' . $nomeFantasia . '</td>
       <td class="center">' . $tel . '</td>
       <td class="center">' . $contato . '</td>
       <td>' . $email . '</td>
       </tr>';


       }
   } else {
       echo ("Problema ao realizar a consulta n banco de dados" . mysqli_error($conexao));
   }

   mysqli_close($conexao);
?>
  • In this particular case you are saying that it does not return the value to variable $sql has what value ? You can consult with var_dump($sql);

  • string(87) "select * from supplier Where razaoSocial like '%new%' or namFantasia like '%new%';"

  • Do you have any record with razaoSocial or nomeFantasia with novo ? When you perform this query on phpMyAdmin get some result?

  • Yes, I only have one record with new nameFantasia and phpmysadmin works correctly. In this table I have several testName and code is working when the survey is test.

1 answer

1


In his if is reading the first row of the resulting table and right after when it will enter the while reads the second line without using the first:

if ($resultado = mysqli_fetch_array($salva)) { //lê a primeira
   while ($resultado = mysqli_fetch_array($salva)) { //aqui lê a segunda

So you lose your first record.

You can correct by changing your if to test the amount of records available:

if (mysqli_num_rows($salva) > 0){
   while ($resultado = mysqli_fetch_array($salva)) { 

Browser other questions tagged

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