How to make searching for my blog with pdo?
I try like this:

<?php require('include/config.php');?>



<?php
    $query = $_GET['query'];
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query);
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM articles
            WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>

But PDO database show error that cant conect in database

Member Avatar for diafol

You probably need a FULLTEXT search: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

Also use mb_strlen() instead of strlen() and any other string functions, use the mb_* alternative if possible.

The query is pretty much the same but instead of using the variable directly in the query - which you shouldn't do anyway (you should sanitize it if using mysql_*) - you bind parameters and use a prepared statement. Example of PDO...

$stmt = $db->prepare("SELECT... MATCH(field1,field2,field3) AGAINST (:keyword)");
$stmt->execute(array(':keyword'=>$keyword);
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);

Your results are then in the $res array - so you can use a loop structure (foreach or even while) to extract records into whichever format you like.

I do like this but have errors:

<?php require('includes/config.php'); ?>


<?php 
 $query = $_GET['query'];
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(mb_strlen($query) >= $min_length){ 
     $stmt = $db->prepare("SELECT FROM blog_post_seo MATCH(postTitle,postDesc,postCont) AGAINST (:keyword)");
    $stmt->execute(array(':keyword'=>$keyword));
    while($row = $stmt->fetch()){
        echo $row['postTitle'];
    }



}
 ?>

error: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

Member Avatar for diafol

You haven't assigned $keyword...

$keyword = $_GET['query'];

May do it.

Also, you may to index your fulltext fields. In addition, I think that only MyISAM tables support fulltext searching. I may be a bit out of date with this though.

//EDIT

5.6.4 now supports InnoDB FTS - sorry.

Oh i try but the same error :/
When put only 2 letter show mi cant find but when is more letter error.

<?php require('includes/config.php'); ?>


<?php 
     $keyword = $_GET['query'];
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(mb_strlen($keyword) >= $min_length){ 
     $stmt = $db->prepare("SELECT FROM blog_post_seo MATCH (postTitle, postDesc, postCont) AGAINST (:keyword)");
    $stmt->execute(array(':keyword'=>$keyword));
    while($res = $stmt->fetchAll(PDO::FETCH_ASSOC)){
        echo $row['postTitle'];
         echo $row['postDesc'];
          echo $row['postCont'];
    }




}
 else {
        echo 'Cant find! ';
    }
 ?>
Member Avatar for diafol

fulltext search needs 4 characters I think. Searching for less doesn't make much sense.

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.