我正在尝试在我的sql数据库上进行搜索,但它没有返回任何内容。当我使用var_dump
时,它给了我"string(0("我的PHP代码似乎没问题,谁能帮我弄清楚?
代码:
功能.php:
function buscarCarros($pConexao, $nome){
$carro = array();
$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[] = $nomeResultado;
}
mysqli_stmt_close($stmt);
}
return $carro;
}
result_pesquisa.php:
<?php
include('header.php');
include_once('menu.php');
include_once('conecta.php');
include_once('functions.php');
$nome = $_REQUEST['pesquisas'];
$carro = buscarCarros($conexao, $nome);
foreach ($carro as $carros) {
echo $carros['nome'];
}
?>
和搜索框,菜单.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>
由于我还没有足够的声誉来评论,我将在这里写下我的建议。
SQL对我来说似乎有点奇怪。 SELECT nome FROM carros WHERE nome = ?
.对我来说,你似乎在选择你已经知道的唯一事情。
我会尝试的另一件事是使用 $_POST 超级全局而不是 $_REQUEST 全局,因为后者在每个请求启动期间都会受到 PHP 的影响。文档在这里。
PHP 本身看起来不错(很高兴看到有人使用预准备语句......太好了!并且根据 HTML 验证器,HTML 是有效的,假设您将文档类型和字符编码声明为 UTF-8。