Problems with database query (Search system)

Asked

Viewed 51 times

2

I’m trying to create a search system in db however this returning only one result, the last address in the table (I had managed to make them all return, however after changing the layout I went to test and gave no more, even erasing 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 am using the bootstrap and would like them to return in the following way:

<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>

1 answer

1


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

Imagine a table whose column is called "user name" and the arrow (->) is the pointer of the list.

-> Beltrano Tal
   Ciclano Soares
   Fulano Silva

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

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

At this time the variable $name takes on the value Beltrano Tal and the pointer moves one position forward, and our list is as follows:

   Beltrano Tal
-> Ciclano Soares
   Fulano Silva

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

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

When executing the while the value of the variable $nome 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, the way it is above the names will all return together. It is up to you to decide how the variable $nome should be built for later presentation. For example:

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

and in the place that should be presented for example:

echo "<ul>".$nome."</ul>";

Browser other questions tagged

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