Problems with the query in the database (Search system)

2

I am trying to create a search system in db however it is returning only one result, the last address in the table (I had managed to get them all returned, however after changing the layout I went to test and did not give more, even deleting everything and returning from when it worked).

<?php
$host="localhost";
$user="root";
$password="";
$db="db";
$con = new mysqli($host,$user,$password,$db);

$verifica = 0;
$city = $_POST['city'];
$bairrop = $_POST['bairrop'];

if(!empty($_POST['city']) && !empty($_POST['bairrop'])){
$result_search = "SELECT * FROM user WHERE cidade LIKE '%$city%' AND bairro LIKE '%$bairrop%'";
$resultado_search = mysqli_query($con, $result_search);
$verifica = mysqli_num_rows($resultado_search);
}elseif (!empty($_POST['city'])) {
$result_search = "SELECT * FROM user WHERE cidade LIKE '%$city%'";
$resultado_search = mysqli_query($con, $result_search);
$verifica = mysqli_num_rows($resultado_search);
}elseif (!empty($_POST['bairrop'])) {
$result_search = "SELECT * FROM user WHERE bairro LIKE '%$bairrop%'";
$resultado_search = mysqli_query($con, $result_search);
$verifica = mysqli_num_rows($resultado_search);
}


if($verifica > 0){
  while($row_search = mysqli_fetch_array($resultado_search)) {
  $nome = $row_search['nome_usuario'];
}
}else{
  echo "Nenhum resultado encontrado.";
}
?>

2: I'm using bootstrap and would like them to return as follows:

<div class="media">
<div class="media-left">
<a href="#">
  <img class="media-object" src="AVATAR" alt="...">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">AQUI O NOME</h4>
DESCRIÇÃO
</div>
</div>
    
asked by anonymous 04.04.2017 / 01:14

1 answer

1

The mysql_fetch_array () function returns the value where the pointer is, and after returning the value, moves the pointer forward.

Imagine a table whose column is named "user_name" and the arrow (->) is the pointer to the list.

-> Beltrano Tal
   Ciclano Soares
   Fulano Silva

When we use the mysql_fetch_array () function, it returns the first line:

   while($row_search = mysqli_fetch_array($resultado_search)) {
   $nome = $row_search['nome_usuario'];

At this point the $ name variable takes the value Beltrano Tal and the pointer moves forward, and our list looks like this:

>
   Beltrano Tal
-> Ciclano Soares
   Fulano Silva

At this point the $ name variable takes the value Cyclano Soares and the pointer moves one position forward, and so on.

  

So that's why in the while in the variable $nome only the last name appears.

When running while , the value of the $nome variable is being overwritten because you are not concatenating the values.

You can for example do so to concatenate

$nome .= $row_search['nome_usuario'];

or

$nome = $nome.$row_search['nome_usuario'];

Of course, as it is above the names will all return together. It's up to you to decide how the $nome variable should be built for later presentation. For example:

'$nome .= "<li>" . $row_search['nome_usuario'] . "</li>";'

and in the place that should be displayed for example:

echo "<ul>".$nome."</ul>";
    
04.04.2017 / 02:30