PHP网站数据库搜索-未显示mySQL查询结果

时间:2018-11-05 05:55:47

标签: php mysql database

我正在设计一个带有搜索栏的网站以输入查询。但是,每当输入查询时,我都会得到一个空白页。

在index.php中,我有:

<?php
include_once('search.php');
?>

<head>
<title>Search</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
  <form method="POST" action="search.php">
  <input type="text" name="q" placeholder="Enter query"/>
  <input type="submit" name="search" value="Search" />
  </form>
</body>

在search.php中,我有

<?php
include_once('db.php'); //Connect to database
if(isset($_POST['search'])){
  $q = $_POST['q'];
  $query = mysqli_query($conn, "SELECT * FROM 'words' WHERE 'englishWord' LIKE '%$q%'");
  $count = mysqli_num_rows($query);
  if($count == "0"){
    $output = '<h2>No result found</h2>';
}else{
  while($row = mysqli_fetch_array($query)){
  $s = $row['yupikWord']; //column of results
    $output .= '<h2>'.$s.'</h2><br>';
  }
}
}
echo $output;
mysqli_close($conn);
?>

在db.php中,我有

<?php
$servername = "localhost";
$username = "qaneryararvik";
$password = "PASSWORD";
$database = "yupik";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);
$dbcon = mysqli_select_db($conn,$database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully<br>";

我可以在搜索框中输入文字,然后单击“搜索”按钮。当我单击“搜索”时,我只得到一个空白页面,上面写着“已成功连接”。查询结果不显示。我在做什么错了?

2 个答案:

答案 0 :(得分:1)

报价错误。对于表和行,应为`而不是'

"SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%'"

答案 1 :(得分:0)

在search.php中应该在下面

include_once('db.php'); //Connect to database
$output = "";
if(isset($_POST['search'])){
   $q = $_POST['q'];
   $sql ="SELECT * FROM words  WHERE englishWord LIKE '%$q%'";

if ($result=mysqli_query($conn,$sql))
{
   while($row = mysqli_fetch_array($result)){
   $s = $row['yupikWord']; //column of results
   $output .= '<h2>'.$s.'</h2><br>';
}
   mysqli_free_result($result);
}else{
   $output = '<h2>No result found</h2>';
}
mysqli_close($conn);
}else{
   $output = '<h2>No result found</h2>';
}

echo $output;