<?php
if(!empty($_GET['q'])) {
    search();
}

function search() {
    $con = mysql_connect('localhost','root', '');
    mysql_select_db('mydb', $con);

    $q = mysql_real_escape_string($_GET['q'], $con);
    $sql = mysql_query("
        SELECT
            p.title, SUBSTR(p.post,1,300) as post
        FROM Posts p
        WHERE p.title LIKE '%{$q}%' OR p.post LIKE '%{$q}%'
        ");

    //Create an array with the results (myframe)
    $myframe=array();
    while($v = mysql_fetch_object($sql)){
        $myframe[] = array(
          'title'=>$v->title,
          'post'=>$v->post
        );
    }

    //using JSON to encode the array
    echo json_encode($myframe);
}
