1

Here is some php code I made(comes with database). My problem is when I search it shows me only 1 result from the table even though they are more. for example the table has names(david star and david plumber) if I search for david it displays only one of both instead of both. Would really appreciate any possible help. Thanks.

<?php 
    //load database connection
    $host = "localhost";
    $user = "root";
    $password = "";
    $database_name = "autocomplete";

    $pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, 
                $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ));


    // OR author LIKE '%$search%'  LIMIT 0 , 10
    $search=$_POST['studentnum'];
    $query = $pdo->prepare("select * 
                            from search, search_and_highlight 
                            where Name LIKE '%$search%'");

    $query->bindValue(1, "%$search%", PDO::PARAM_STR);
    $query->execute();

    // Display search result
    if (empty($search)) {

        echo "<p>You forgot to enter a search term!!!";

    } else
        if ( $results = $query->fetch()) {

            echo ' <div class="tm-sidebar-pad-2">
                      <a href="#" class="media tm-media tm-recommended-item">
                         <img src="img/img-01.jpg" alt="Image">
                         <div class="media-body tm-media-body tm-bg-gray">
                              <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">'.$results['Name'].'</h4>
                              <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">
            echo    </h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Kategorie</h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Kategorie</h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">. </h4>
                    <h4 class="text-uppercase tm-font-semibold tm-sidebar-item-title">Preis</h4>
                </div>                                        
            </a>
        </div>';

    } else {
        echo 'Nothing found';
    }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 4
    you need to `loop` through your result. – Badiparmagi Jan 31 '18 at 10:47
  • You also need to look at your query! The `bindValue` is not doing anything and therefore your script is open to SQL Injection attack – RiggsFolly Jan 31 '18 at 10:48
  • 1
    Your echo of HTML is also incorrect – RiggsFolly Jan 31 '18 at 10:52
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 31 '18 at 10:53
  • Your script is wide open to [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jan 31 '18 at 10:54
  • OK thanks @Badiparmagi it worked – Sah Versheri Jan 31 '18 at 10:57

1 Answers1

0

To get multiple records, you need to loop result record set:

while ($result = $stmt->fetch( PDO::FETCH_ASSOC )) {
  print $result['Name'];
}
Sky
  • 90
  • 7