0

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?

5
  • 1
    You really shouldn't post passwords. You should delete this and start over. Even if you edit, people can see the edits. Commented Nov 5, 2018 at 6:02
  • Possible duplicate of When to use single quotes, double quotes, and back ticks in MySQL Commented Nov 5, 2018 at 6:03
  • use print_r($count) to see if there is a record or not Commented Nov 5, 2018 at 6:06
  • What have you tried to debug the problem? Have you even tried to detect any error thrown by the database? Commented Nov 5, 2018 at 7:56
  • Remove <?php include_once('search.php'); ?> from your index.php Commented Nov 5, 2018 at 10:07

2 Answers 2

1

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

"SELECT * FROM `words` WHERE `englishWord` LIKE '%$q%'"
Sign up to request clarification or add additional context in comments.

Comments

0

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;

1 Comment

Please highlight the differences and explain why they could solve the problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.