2

I am currently building a website that relies on pulling information from my localhost database through a search bar. I currently have it displaying all the information correctly, but when a search doesn't match anything in my database then it just shows nothing, how do I change this to "No results found" or something of the sort? My code is:

<?php

    require_once 'connect.php';

    if(isset($_GET['keywords'])){

        $keywords = $db->escape_string($_GET['keywords']);  

        $query = $db->query("
                        SELECT movie, game
                        FROM movies
                        WHERE movie LIKE '%{$keywords}%'
                     ");
?>

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>

<body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
        <form action="search.php" method="get">
            <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
            <input type="submit" value="Search!">
        </form>
    </div>
<br />
<?php

    if($query->num_rows){
        while($r = $query->fetch_object()){
        ?>
    <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
    <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
</body>
</html>
<?php
    }
}

}

Sorry for pasting literally the full page, but I don't even know where to start as I'm a PHP noob.

1
  • if($query->num_rows){...}else{echo "NO RESULT FOUND";} Commented Dec 16, 2015 at 19:23

3 Answers 3

4

if($query->num_rows){..} condition will be true if query result have more then 0 results, so just use else part to print message that no result(s) found

<?php

    require_once 'connect.php';

    if(isset($_GET['keywords'])){

    $keywords = $db->escape_string($_GET['keywords']);  

    $query = $db->query("
    SELECT movie, game
    FROM movies
    WHERE movie LIKE '%{$keywords}%'
    ");
    ?>

    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>

    <body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
      <form action="search.php" method="get">
        <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
        <input type="submit" value="Search!">
      </form>
    </div>
    <br />
    <?php

    if($query->num_rows){
        while($r = $query->fetch_object()){
            ?>
    <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
    <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
    </body>
    </html>
    <?php
        }
    }
    else{
        echo "No Result Found!";
        }

    }
Sign up to request clarification or add additional context in comments.

Comments

0
<?php

require_once 'connect.php';

if(isset($_GET['keywords']))
{
    $keywords = $db->escape_string($_GET['keywords']);  
    $query = $db->query("SELECT movie, game FROM movies WHERE movie LIKE '%{$keywords}%'");
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Example</title>
    <link rel="stylesheet" type="text/css" href="styles.css">
    </head>
    <body>
    <h1><a href="index.php">Test</a></h1>
    <div class="search">
        <form action="search.php" method="get">
          <input name="keywords" type="text" placeholder="Type something!" autocomplete="off" size="40">
          <input type="submit" value="Search!">
        </form>
    </div>
    <br />
    <?php
    if($query->num_rows)
    {
        while($r = $query->fetch_object())
        {
        ?>
        <div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
        <div class="result"> <a><?php echo $r->game; ?></a> </div><br />
        <?}
    }
    else {?>
     <div class="noResult">No Results Found</div>
    <?}?>
    </body>
    </html>
<?php }?>

styles.css (Here, using this class name, whatever property you want you can set. I gave few. Example.)

.noResult{
color: #800000;
font-size:14 px;
}

2 Comments

How would I go about stylising the "No results found" in CSS? What should I put inside a div?
I've updated my answer. Please have a look. You need to create one <div></div> in page. See my answer. @DavidDuffy95
0

to fit with your opening and closing tags you can use:

if($query->num_rows){
    while($r = $query->fetch_object()){
    ?>
<div class="movie"> <a><?php echo $r->movie; ?></a> </div><br />
<div class="result"> <a><?php echo $r->game; ?></a> </div><br />

<?php
    } 
} else {
    ?>
        <div>No results</div>
    <?php
}
?>
</body>
</html>

do keep the /body and /html tags after any output.

1 Comment

I get a Syntax error when I do this. Any ideas why?

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.