Hello,
I am trying to create a search form in order to search articles title and contents. Here is my code:
searchPDO.php
<?php
// PDO start here
//This is only displayed if they have submitted the form
$find = strip_tags(@$_POST['find']);
$searching = strip_tags(@$_POST['searching']);
if (isset($searching) && $searching==yes)
{
echo "<h2>Results</h2><p>";
//If they did not enter a search term we give them an error
if ($find == "")
{
echo "<p>You forgot to enter a search term";
exit;
}
// Otherwise we connect to our Database
// #
// We preform a bit of filtering
$find = strtoupper($find);
$find = strip_tags($find);
$find = trim($find);
$user = 'indonusa';
$pass = '12345';
try {
$pdo = new PDO('mysql:host=localhost;dbname=indonusacms', $user, $pass);
}
catch (PDOException $exception) {
// unlike mysql/mysqli, pdo throws an exception when it is unable to connect
echo '<p>There was an error connecting to the database!</p>';
if ($pdoDebug) {
// pdo's exception provides more information than just a message
// including getFile() and getLine()
echo $exception->getMessage();
}
}
$query = "SELECT * FROM static_content AND dynamic_content WHERE upper(images) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'";
try {
$pdoStatement = $pdo->query($query);
}
catch (PDOException $exception) {
// the query failed and debugging is enabled
echo "<p>There was an error in query: $query</p>";
echo $exception->getMessage();
$pdoStatement = false;
}
if ($pdoStatement) {
// perhaps you want to check if there are any rows available
if ($pdoStatement->rowCount() == 0) {
echo '<p>No records found.</p>';
}
else {
while ($recordObj = $pdoStatement->fetchObject()) {
echo $recordObj->mycolumn;
}
}
/*
foreach($dbh->query("SELECT * FROM static_content WHERE upper(image) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'") as $data)
{
echo '<img src="images/events/thumb/' . $data['images'] . '">';
echo " ";
echo $data['title'];
echo "<br>";
echo $data['content'];
echo "<br>";
echo "<br>";
}
*/
$pdoStatement->closeCursor();
}
// clean up any objects
unset($pdoStatement);
unset($pdo);
// PDO end here
//This counts the number or results - and if there wasn't any it gives them a little message explaining that
$anymatches=$pdoStatement->rowCount();
if ($anymatches == 0)
{
echo "Sorry, but we can not find an entry to match your query<br><br>";
}
//And we remind them what they searched for
echo "<b>Searched For:</b> " .$find;
}
?>
I get this error, when I try to search for : tanggal (a word which exist in one of my content).
Results
Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 103
------------------------------
line 103: $anymatches=$pdoStatement->rowCount();
I wonder why?