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?

You cannot access any properties after you have unset your object.

ok, I move the unset to the bottom.

searchPDO.php

    $pdoStatement->closeCursor();
}

 //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>"; 
 } 

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here

I still receive this error when I try to search a word.

Results

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 95

line 95: $anymatches=$pdoStatement->rowCount();

You also need to move $pdoStatement->closeCursor();

ok, I already move $pdoStatement->closeCursor();

searchPDO.php

}

 //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; 


 $pdoStatement->closeCursor();

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here  

Results

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 94

line 94: $anymatches=$pdoStatement->rowCount();

SELECT * FROM static_content AND dynamic_content WHERE upper(images) LIKE '%$find%' or upper(title) LIKE'%$find%' or upper(content) LIKE'%$find%'

The above is not a valid query. Using FROM static_content AND dynamic_content should probably be FROM static_content, dynamic_content

I still have the following error appears:

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 94

line 94: $anymatches=$pdoStatement->rowCount();

If you don't post your new code and query, I can only guess. Probably still an error in your query.

The code using PDO seems twice as long as the mysql_* .

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 = '*****'; 

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, 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>"; 
    }

    */


}

 //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; 


 $pdoStatement->closeCursor();

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here  

 } 
 ?> 

I suggest you turn on debugging, looks like you removed some parts from my code snippet.

Add $pdoDebug = true; before connecting, and the following after connecting:

if ($pdoDebug) {
    // $pdo->query() returns FALSE if there is an error
    // to get more information, the following will enable exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}

Results

There was an error in query: SELECT * FROM static_content, dynamic_content WHERE upper(images) LIKE '%HALLO%' or upper(title) LIKE'%HALLO%' or upper(content) LIKE'%HALLO%'
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'images' in where clause is ambiguous
Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 102

Column 'images' in where clause is ambiguous

You'll need to explicitly specify the table the images column belongs to. Since both tables have one, you'll need that where clause twice, once for each table.

I suggest you read up on how to create queries correctly.

Results

There was an error in query: SELECT * FROM static_content WHERE upper(images) LIKE '%HALLO%' or upper(title) LIKE'%HALLO%' or upper(content) LIKE'%HALLO%', dynamic_content WHERE upper(images) LIKE '%HALLO%' or upper(title) LIKE'%HALLO%' or upper(content) LIKE'%HALLO%'

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' dynamic_content WHERE upper(images) LIKE '%HALLO%' or upper(title) LIKE'%HALLO%' at line 1

Fatal error: Call to a member function rowCount() on a non-object in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 101

SELECT * FROM static_content, dynamic_content 
WHERE upper(dynamic_content.images) LIKE '%HALLO%' 
or upper(dynamic_content.title) LIKE'%HALLO%' 
or upper(dynamic_content.content) LIKE'%HALLO%'
or upper(static_content.images) LIKE '%HALLO%' 
or upper(static_content.title) LIKE'%HALLO%' 
or upper(static_content.content) LIKE'%HALLO%'

I suggest you read up on how to create queries correctly.

I have changed the query yet I still cannot search a word that exist in the content. (I only change the query)

    $query = "SELECT * FROM static_content, dynamic_content
    WHERE upper(dynamic_content.images) LIKE '%$find%'
    or upper(dynamic_content.title) LIKE'%$find%'
    or upper(dynamic_content.content) LIKE'%$find%'
    or upper(static_content.images) LIKE '%$find%'
    or upper(static_content.title) LIKE'%$find%'
    or upper(static_content.content) LIKE'%$find%'";

Results

No records found.
Sorry, but we can not find an entry to match your query

Searched For: TANGGAL

This is the whole codes:

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); 

$pdoDebug = true; 

$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();
    }
}

    if ($pdoDebug) {
    // $pdo->query() returns FALSE if there is an error
    // to get more information, the following will enable exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

    $query = "SELECT * FROM static_content, dynamic_content
    WHERE upper(dynamic_content.images) LIKE '%$find%'
    or upper(dynamic_content.title) LIKE'%$find%'
    or upper(dynamic_content.content) LIKE'%$find%'
    or upper(static_content.images) LIKE '%$find%'
    or upper(static_content.title) LIKE'%$find%'
    or upper(static_content.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>"; 
    }

    */


}

 //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; 


 $pdoStatement->closeCursor();

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here  

 } 
 ?> 

<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<div id="search">
<input type="text" name="find" placeholder="search.." value="" width="80">
<input type="hidden" name="searching" value="yes" />
</div>

<div id="sbutton">
<input src="images/search button.jpg" name="submit" value="Search" type="image" width="30"></div>
</form>

At line 10 there is an error:

if (isset($searching) && $searching==yes)

the value yes is a string not a constant, so change it to:

if (isset($searching) && $searching=='yes')

At line 82 you wrote:

echo $recordObj->mycolumn;

But you're retrieving title, content and images from the query, so it should be:

echo $recordObj->title;

A part this, it works fine, but I think you need to use UNION instead of the previous query:

$query = "SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%$find%' or upper(static_content.title) LIKE '%$find%' or upper(static_content.content) LIKE '%$find%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%$find%' or upper(dynamic_content.title) LIKE '%$find%' or upper(dynamic_content.content) LIKE '%$find%'";

Otherwise it can return all the rows from both the tables, not only the correct matches. Spot the differences.

Docs: http://dev.mysql.com/doc/refman/5.5/en/union.html

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); 

$pdoDebug = true; 

$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();
    }
}

    if ($pdoDebug) {
    // $pdo->query() returns FALSE if there is an error
    // to get more information, the following will enable exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

        $query = "SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%$find%' or upper(static_content.title) LIKE '%$find%' or upper(static_content.content) LIKE '%$find%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%$find%' or upper(dynamic_content.title) LIKE '%$find%' or upper(dynamic_content.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->title;
        }
        }
    /*
    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>"; 
    }

    */


}

 //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; 


 $pdoStatement->closeCursor();

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here  

 } 
 ?> 

I try to search a word that exist in the content and it still cannot find the word that I search.

Results

No records found.
Sorry, but we can not find an entry to match your query

Searched For: TANGGAL

Try to print the query and run on command prompt. See what are you getting there.

I don't know run this thing on command prompt.

This is the print query:

print_r($query);

Results

SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%TANGGAL%' or upper(static_content.title) LIKE '%TANGGAL%' or upper(static_content.content) LIKE '%TANGGAL%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%TANGGAL%' or upper(dynamic_content.title) LIKE '%TANGGAL%' or upper(dynamic_content.content) LIKE '%TANGGAL%'

davy_yg,

There is a sure shot in checking if the query is indeed valid. All you have to do is get on to your phpMyAdmin page, click on the database of which your query is adressed or intended to be executed, click on the SQL tab and then paste your query without double quotes and then hit the "go" button. If your query is written properly, then it should show some results.

Another effecient method in perfecting your mySQL fluency is to use the mySQL workbench from oracle. Connect to your target database and then paste your query to check if it is a valid one. You can also optimized your query if needed. Copy the optimized query and plug it in to your PHP script.

There are many things you can do in the workbench that you can't do in running a query through the PHP script.

I try pasting this query to my phpmyadmin SQL Tab:

SELECT static_content.title, static_content.content, static_content.images
FROM static_content
WHERE upper( static_content.images ) LIKE '%TANGGAL%'
OR upper( static_content.title ) LIKE '%TANGGAL%'
OR upper( static_content.content ) LIKE '%TANGGAL%'
UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images
FROM dynamic_content
WHERE upper( dynamic_content.images ) LIKE '%TANGGAL%'
OR upper( dynamic_content.title ) LIKE '%TANGGAL%'
OR upper( dynamic_content.content ) LIKE '%TANGGAL%'
LIMIT 0 , 30

and it pull out this data:

title content images
Sunvone Bantu Kemendagri Dalam Project e-KTP <p class="MsoNormal" style="text-align: left;"><sp... 405703507611490342.jpg

That's correct. Exactly as I want it. Yet, I wonder why it doesn't work in when I place the query in my SearchPDO.php file ?

Can you show the output of:

show create table dynamic_content;
show create table static_content;

It could be a problem of encoding, for example latin1 from your browser and utf8 in your tables, in this case you can force the form to submit the same encoding of the tables by adding the accept-charset attribute, for example:

<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" accept-charset="utf-8">

dynamic_content

CREATE TABLE dynamic_content (\n ID int(20) NOT NULL auto_increment,\n CatID int(5) NOT NULL default '0',\n frontpage int(2) NOT NULL default '0',\n author varchar(20) NOT NULL default '',\n content text NOT NULL,\n eng_content text NOT NULL,\n title varchar(50) NOT NULL,\n thumb varchar(30) NOT NULL default '',\n images varchar(30) NOT NULL default '',\n date date NOT NULL default '0000-00-00',\n status int(2) NOT NULL default '0',\n english text NOT NULL,\n PRIMARY KEY (ID)\n) ENGINE=MyISAM DEFAULT CHARSET=latin1

static_content

CREATE TABLE static_content (\n ID int(20) NOT NULL auto_increment,\n menu_id varchar(20) default NULL,\n content text NOT NULL,\n eng_content text NOT NULL,\n shorttext varchar(100) default NULL,\n images varchar(30) NOT NULL default '',\n top int(2) NOT NULL default '0',\n title varchar(50) default NULL,\n PRIMARY KEY (ID)\n) ENGINE=MyISAM DEFAULT CHARSET=latin1

So, have you tried to force it through the form? Since the charset of the tables is latin1 you should use iso-8859-1 instead of utf-8:

<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" accept-charset="iso-8859-1">

Otherwise you could convert the tables to utf8:

ALTER TABLE dynamic_content DEFAULT CHARACTER SET utf8;
ALTER TABLE static_content DEFAULT CHARACTER SET utf8;

I don't see anything else that could be wrong, in my test your script works fine.

I still get:

Results

SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%TANGGAL%' or upper(static_content.title) LIKE '%TANGGAL%' or upper(static_content.content) LIKE '%TANGGAL%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%TANGGAL%' or upper(dynamic_content.title) LIKE '%TANGGAL%' or upper(dynamic_content.content) LIKE '%TANGGAL%'

No records found.
Sorry, but we can not find an entry to match your query

Searched For: TANGGAL

---------------------------

This is the whole codes for 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); 

$pdoDebug = true; 

$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();
    }
}

    if ($pdoDebug) {
    // $pdo->query() returns FALSE if there is an error
    // to get more information, the following will enable exceptions
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }

        $query = "SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%$find%' or upper(static_content.title) LIKE '%$find%' or upper(static_content.content) LIKE '%$find%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%$find%' or upper(dynamic_content.title) LIKE '%$find%' or upper(dynamic_content.content) LIKE '%$find%'";

        print_r($query);

        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->title;
        echo $recordObj->content;
        echo $recordObj->images;
        }
        }
    /*
    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>"; 
    }

    */


}

 //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; 


 $pdoStatement->closeCursor();

 // clean up any objects
 unset($pdoStatement);
 unset($pdo);

 // PDO end here  

 } 
 ?> 

<form name="contact" action="<?php $_SERVER['PHP_SELF'] ?>" method="POST" charset="iso-8859-1">
<div id="search">
<input type="text" name="find" placeholder="search.." value="" width="80">
<input type="hidden" name="searching" value="yes" />
</div>

<div id="sbutton">
<input src="images/search button.jpg" name="submit" value="Search" type="image" width="30"></div>
</form>

I still unable to search a word that exist in the content.

Ok, something I wasn't considering: rowCount() does not always return the numbers of rows of a select query, this works only for insert, update and delete queries.

From the docs:

If the last SQL statement executed by the associated PDOStatement was a SELECT statement, some databases may return the number of rows returned by that statement. However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

To get the total you have to run a secondary query, this can be run previously to the main to check the number of rows, otherwise you can use FOUND_ROWS() after the execution of the query, MySQL will return the total. So in order to work with your script change line 73:

if ($pdoStatement->rowCount() == 0) {

with:

$countRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();
if ($countRows == 0) {

And line 102:

$anymatches=$pdoStatement->rowCount();

with:

$anymatches = $countRows;

Note that FOUND_ROWS() is available only on MySQL and forks, so if you decide to change database you will have to modify your script logic.

Reference:

Results

SELECT static_content.title, static_content.content, static_content.images from static_content WHERE upper(static_content.images) LIKE '%CONTENT%' or upper(static_content.title) LIKE '%CONTENT%' or upper(static_content.content) LIKE '%CONTENT%' UNION SELECT dynamic_content.title, dynamic_content.content, dynamic_content.images FROM dynamic_content WHERE upper(dynamic_content.images) LIKE '%CONTENT%' or upper(dynamic_content.title) LIKE '%CONTENT%' or upper(dynamic_content.content) LIKE '%CONTENT%'

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.' in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php:76 Stack trace: #0 E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php(76): PDO->query('SELECT FOUND_RO...') #1 {main} thrown in E:\xampp\xampp\htdocs\IndonusaCMS\includes\searchPDO.php on line 76

Line 76: $countRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();

Uhm. I think I cannot help much more, I believe that all these differences happens because, in my case, PDO uses the MySQL Native Driver, while yours is probably using libmysqlclient:

You could try to fetch the results before performing the second query, so:

$result = $pdoStatement->fetchAll();
$countRows = $pdo->query("SELECT FOUND_ROWS()")->fetchColumn();
if ($countRows == 0) {

And then the while loop would become a foreach loop:

foreach($result as $row => $column)
{
    echo $column['title'];
    echo $column['content'];
    echo $column['images'];
}

But if it does not work wait for appropriate help.

commented: it works +4

Hey, thanks. Now, it works.

I just wonder why using the PDO thing, is twice as long as normal mysql_* ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.