mysqli receiving Boolean

Asked

Viewed 156 times

0

My site is not searching! I couldn’t figure out how to make queries through the search box, I try to access the cars in the database and it brings me the following error:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/u374984363/public_html/newcars/functions.php on line 61

my codes:

functions.php :

function buscarCarros($conexao, $nome){
    $carro = array();
    $conexao = mysqli_connect('mysql.hostinger.com.br','u374984363_ozzy','******', 'u374984363_ncars');
    $query = "select * from carros where nome = {$carro}";
    $resultado = mysqli_query($conexao, $query);
    while ($carros = mysqli_fetch_assoc($resultado)) {
        array_push($carro, $carros);
    }
    return $carro;
}

result_pesquisa.php :

<?php
include('menu.php');
include('conecta.php');
include('functions.php');

$carro = buscarCarros($conexao, $nome);
    foreach ($carro as $carros) :
        $carros['nome'];
?>


menu.php :

        <nav class="twelve columns">
        <ul class="menu">
            <li class="menu-item"><a href="index.php">Home</a></li>
            <li class="menu-item"><a href="comprar.php">Comprar</a></li>
            <li class="menu-item"><a href="">Vender</a></li>
            <li class="menu-item"><a href="">Contato</a></li>
        </ul>
        <form id="searchbox" action="result_pesquisa.php" method="POST">
            <input type="text" class="search-top remove-bottom" name="pesquisas" placeholder="Qual carro você procura?">
            <!-- <a href="javascript:document.getElementById('searchbox').submit();"> -->
            <a href="result_pesquisa.php"><span class="icon-top icon-search"></span></a>
        </form>
    </nav>
  • Place var_dump($conexao->error); under the mysqli_query to see what the mistake was.

  • Change your database password.

  • Changed ! Sorry for the error... I did the right thing and got this: string(40) "Unknown column 'Array' in 'Where clause'" Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in /home/u374984363/public_html/newcars/functions.php on line 62

  • Change where it says {$carro} for '{$nome}' in your query and see what happens.

  • I got this: string(148) "You have an error in your SQL syntax; check the manual that Corresponds to your Mariadb server version for the right syntax to use near '' at line 1" Warning: mysqli_fetch_assoc() expects Parameter 1 to be mysqli_result, Boolean Given in /home/u374984363/public_html/newcars/functions.php on line 62.. it seems that this code does not want to run :/

  • Put the simple quotes also, as it is in the comment?

  • Now yes and he brought me this: string(0) ""... what happened ? he did not find what I searched ? by looking in the database, the name was Thank you !

  • I’ll put it in the answer, you’ll see.

  • Okay! Thank you, I’ll be waiting !

Show 4 more comments

1 answer

1

You are going to array $carros for the database table name field, I imagine you wanted to pass the variable $nome instead.

functions.php
Use Prepared statements to avoid sql injections attack. Read the discussion on the two subjects, in a question from S.O. PT.

function buscarCarros($pConexao, $nome){
    $carro = array();
    //$conexao = mysqli_connect('mysql.hostinger.com.br','u374984363_ozzy','******', 'u374984363_ncars');
    // a variável $conexao deve vir do 'conecta.php', não precisamos conectar de novo
    $query = "SELECT nome FROM carros WHERE nome = ?";

    if ($stmt = mysqli_prepare($pConexao, $query)) {

        mysqli_stmt_bind_param($stmt, "s", $nome);

        mysqli_stmt_execute($stmt);

        mysqli_stmt_bind_result($stmt, $nomeResultado);

        while (mysqli_stmt_fetch($stmt)) {
            $carro[]['nome'] = $nomeResultado;
        }

        mysqli_stmt_close($stmt);
    }

    return $carro;
}  

result_search.php

include_once('menu.php');
include_once('conecta.php');
include_once('functions.php');
//use include_once ao invés de include quando puder, para evitar de algum dia incluir 
//sem querer 2 vezes o mesmo arquivo, que pode gerar fatal errors e interromper   
//a execução dos scripts caso você esteja declarando alguma função nesses arquivos  

//pegamos o conteúdo que foi pesquisado
$nome = $_REQUEST['pesquisas'];

$carro = buscarCarros($conexao, $nome);
foreach ($carro as $carros) {
    echo $carros['nome'];
}  

In the menu.php failed to put the method attribute of <form> and a Ubmit button, rather than a link that just takes the page to another:

<nav class="twelve columns">
        <ul class="menu">
            <li class="menu-item"><a href="index.php">Home</a></li>
            <li class="menu-item"><a href="comprar.php">Comprar</a></li>
            <li class="menu-item"><a href="">Vender</a></li>
            <li class="menu-item"><a href="">Contato</a></li>
        </ul>
        <form id="searchbox" action="result_pesquisa.php" method="POST">
            <input type="text" class="search-top remove-bottom" name="pesquisas" placeholder="Qual carro você procura?">
            <input type="submit" value="Pesquisar" />
        </form>
 </nav>
  • I do not know the reason, but the bottom of my site turned completely black, and some icons changed... the search also did not return results...

Browser other questions tagged

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