Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

PHP database search query?

I'm working on a php script to search and display results from a sqlite database. I've created a database with 3 columns: 'Name', 'X', and 'Y' under the table name 'Directory'. I have an input box named 'search'. When a name is searched and found under the 'Name' column in the database, the results will be displayed on a simple table.

I have all the files and the database.db file all in the same folder.

The script seems to be connecting to the database file, but it doesn't return results even if there should be a successful hit. I always get the 'Nothing Found' result. Am I missing something?

Any help would be greatly appreciated.

Thank you.

<?php

//load database connection

$myPDO = new PDO('sqlite:database.db');

// Search from SQL database table

$search=$_POST['search'];

$query = $myPDO->prepare("select * from Directory where Name = '$search'");

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

$query->execute();

// Display search result

if (!$query->rowCount() == 0) {

echo "Search found :<br/>";

echo "<table>";

echo "<tr><td>Name</td><td>X</td><td>Y</td></tr>"

while ($results = $query->fetch()) {

echo "<tr><td>";

echo $results['Name'];

echo "</td><td>";

echo $results['X'];

echo "</td><td>";

echo "$".$results['Y'];

echo "</td></tr>";

}

echo "</table>";

} else {

echo 'Nothing found';

}

?>

1 Answer

Relevance
  • Anonymous
    4 years ago
    Favorite Answer

    Try having the script print the value of

    !$query->rowCount()

    and

    !$query->rowCount() == 0

    I think you'll find it's not evaluating the way you think it is/should be.

Still have questions? Get your answers by asking now.