未显示mysql查询结果

fcy6dtqo  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(326)

我正在设计一个网站与搜索栏进入查询。但是,每当我输入一个查询时,就会得到一个空白页。
在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>";

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

cedebl8k

cedebl8k1#

在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;
lmvvr0a8

lmvvr0a82#

报价是错误的。对于表和行,应为“而不是”:

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

相关问题