Creating a PHP Search




This is a tutorial which goes over how to create a PHP search which filters out results from a database table. Sorry for the mistakes made in this video, the video …

Original source


30 responses to “Creating a PHP Search”

  1. Hello, Mr. Smith.
    You can explain very clear php possibilities. Your example works with me very stable with a few moderations. Can you explain how to grab data from table generated after search and use it for example to insert in another table?

  2. <?php

    mysql_connect("localhost","root" ,"admin")or die ("could not connect");
    mysql_select_db("sentmentanalysis" )or die ("could not find db");
    $output= '' ;

    if(isset($_POST['seach'])){
    $searchq = $_POST['seach'];
    $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);
    $query = mysql_query("SELECT * FROM post WHERE Title LIKE '%$$searchq%'OR Contect LIKE '%$searchq%'") or die ("could not search");
    $count = mysql_affected_rows($query);
    if ($count ==0){
    $output = 'There was no search results' ;
    }else{
    while ($row = mysql_fetch_array($query)){
    $titel = $row['Title'];
    $contect = $row['Contect'];
    $output = '<div>'.$titel.' '.$contect.'</div>';
    }
    }
    }
    ?>

    <html>
    <head>
    <title>Search</title>
    </head>
    <body>
    <form action = "searchHtml.php" method="post">
    <input type="text" name = "search" placeholder = "search …."/>
    <input type="submit" value="OK"/>
    </form>
    <?php echo $output ;?>
    </body>
    </html>

    —–
    no result for this code ? whay

  3. PLEASE HELP! ive encoutererd this kind of problem

    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:wampwwwLibrary System Devb_general_knowledge.php on line 100

    //this are my codes

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "library";

    if(isset($_POST['search']))
    {
    $valueToSearch = $_POST['valueToSearch'];
    $query = "SELECT * FROM `b_general_knowledge` WHERE CONCAT(`id`, `title`, `author`, `ISBN`, `accession_number`, `books_availability`)LIKE '%".$valueToSearch."%'";
    $search_result = filtertable($query);
    }
    else
    {
    $query = "SELECT * FROM 'b_general_knowledge'";
    $search_result = filtertable($query);

    }

    function filtertable($query)
    {
    $connect = mysqli_connect("localhost", "root", "", "library");
    $filter_Result = mysqli_query($connect, $query);
    return $filter_Result;

    }

    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title> General Knowledge </title>

    <!– Link in Css –>
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/background.css" rel="stylesheet"></link>

    </head>
    <body>
    <!header>
    <div align="center" class="page-header">
    <h1><strong>SAINT PAUL UNIVERSITY DUMAGUETE</strong></h1>
    <h1><small>COLLEGE LIBRARY</small></h1>
    <br>
    </br>

    </head>
    <body>
    <!– heading for categories –>
    <div class="panel panel-success" style="background-color:rgb(200,200,200);">
    <h3 align="center" style="font-family:century;">
    <h3><b>General Knowledge</b></h3>
    </div>

    <!– Searching –>
    <div class="col-lg-3" style="padding-right: 10px;">
    <div class="input-group">
    <form action="b_general_knowledge.php" method="post">
    <span class="input-group-btn" style="align-content: center;">
    <input type="text" name="valueToSearch" class="form-control" placeholder="Search Information Of Books Here">
    <input type="submit" name="search" class="btn btn-info" value="Search">
    <br><br>
    <a href="student_borrow_book.php" type="submit" class="btn btn-warning">Borrow Book</a>
    <br><br>

    </div>
    </div>
    </span>

    <!– Tables –>
    <br></br>
    <br>
    <div>

    <table class="table table-bordered table-hover table-striped">
    <tr align="center">

    <td><b>TITLE</td>
    <td><b>AUTHOR</td>
    <td><b>ISBN</td>
    <td><b>ACCESSION NUMBER</td>
    <td><b>BOOKS AVAILABILITY</td>

    </tr>

    <?php while($row = mysqli_fetch_array($search_result)):?>

    <td> <?php echo $row['title'];?> </td>
    <td> <?php echo $row['author'];?> </td>
    <td> <?php echo $row['ISBN'];?> </td>
    <td> <?php echo $row['accession_number'];?> </td>
    <td> <?php echo $row['books_availability'];?> </td>
    </tr>
    <?php endwhile;?>
    </table>
    </form>
    </tr>
    </body>
    </html>

  4. this happend when i click submit btn ( ! ) Warning: mysqli_query() expects at least 2 parameters, 1 given in C:Documents and SettingsracunarDesktopwampwwwMoja stranicaindex.php on line 10
    Call Stack
    # Time Memory Function Location
    1 0.0007 375984 {main}( ) ..index.php:0
    2 0.0279 382160 mysqli_query ( ) ..index.php:10
    HELP

  5. Hey. When I load the page, it gives me this error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%% OR last_name LIKE '%%'' at line 1

  6. How do you display multiple records with the same name ? I mean I've tried also your way but I still it doesn't display multiple results. Maybe because of some coding in java which is included in my code?

Leave a Reply