简体   繁体   中英

PHP website database search - mySQL query results not being displayed

I am designing a website with a search bar to enter queries. However, whenever I enter a query, I get a blank page.

In index.php I have:

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

In search.php I have

<?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);
?>

In db.php I have

<?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>";

I am able to enter text into the search box and click the 'Search' button. When I click 'Search', all I get is a blank page that says 'Connected Successfully'. The query results do not show. What am I doing wrong?

Quotations are wrong. Should be ` instead of ' for tables and rows:

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

In search.php should be below

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM