0

I want to make instant search (google like) on key up Jquery ajax must ass value from HTML input field to PHP and PHP must chec in SQL table named "title" for any words which Begin or Contain the written word/letter,if there isn't anything found it must print the results out in a div.

Here is an example:

enter image description here

The picture explains: Up is the input field and down box is the box for results to be printed,as we can see it is working,but PHP don't want to get data from SQL,and only printing the result for 0 value (Nothing Found) on Bulgarian language.

There is my code:

<?php
    $hostname = "localhost";
    $username = "shreddin";
    $password = "!utf55jyst";
    $databaseName = "shreddin_nation";
    $connect = new mysqli($hostname, $username, $password, $databaseName);
    $fsearch = "";

    if (!empty($_POST['fsearch'])) {
        $fsearch = $_POST['fsearch'];

        $req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");

        $req->bind_param('s', $fsearch);
        $req->execute();
        if ($req->num_rows == 0) {
            echo 'Не бяха намерени резултати!';
        }
        else {
            while ($row = $req->fetch_array()) {
                ?>
                    <div class = "search-result">
                        <span class = "result-title">
                            <? php
                                echo $row['title'];
                            ?>
                        </span><br>
                    </div>
                <?php
            }
        }
    }
?>

The code is working till else {...} only this part didn't work..;/ I tried to use echo some results after else {...} because i thought it was a problem with my code,but it didn't work either way ...Can somebody explain to me where is my mistake (with simple language please) i am not really good at coding exept with PHP.

I won't put Jquery and HTML here because all working fine there, the post method is all good, the problem is with the php. But of course if you need it to help me I will paste it with no problem.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99

2 Answers2

1

Edited

$value = '%'.$fsearch.'%;
$req->bind_param('s', $value);

it will work :)

jesuisgenial
  • 685
  • 1
  • 5
  • 15
  • Please, try to give more explanations. You can also read this https://stackoverflow.com/help/how-to-answer – Ortomala Lokni Feb 12 '17 at 09:38
  • It gives me error Cannot pass parameter 2 by reference,guess its because i don't need to pass directly value to bind_param,will try to fix it if i can and will post answer here.Thanks! – Tsvetomirov Yordan Feb 12 '17 at 09:45
  • hey i just edited my answer. let me know if it works :) – jesuisgenial Feb 12 '17 at 10:00
  • You missed 1 ' at the end of $value :D and Still not working,got the exact same effect,it is only displaying "Не бяха намерени резултати" no matter what i type.Thanks for response <3 – Tsvetomirov Yordan Feb 12 '17 at 11:04
  • i just looked your code again and I think found the real problem!. I think you missed $req->store_result(); check out the link :) http://php.net/manual/en/mysqli-stmt.num-rows.php – jesuisgenial Feb 12 '17 at 11:15
  • I checked your link,my code looks like http://pastebin.com/T4amd5fR now,and printing only Results are there is a pic of it http://prntscr.com/e7pg77 Sorry for Non-Stop disturbance mate,and thanks once again <3 Will try to solve it by myself,if you don't have the time to waste with me <3 – Tsvetomirov Yordan Feb 12 '17 at 11:25
  • sorry my answer was so rough. i had no time.. though to give you more hands, I got another suspected point. its about character encoding. Try to search string written in english or numbers – jesuisgenial Feb 12 '17 at 12:00
  • I am trying for like 1 month to do this,lol i learned so many things and still nothing works,i will definitely not surrender,and post my answer here when i am done ! – Tsvetomirov Yordan Feb 13 '17 at 07:28
0

<?php
$hostname = "localhost";
$username = "username";
$password = "pass";
$databaseName = "dbName";
$connect = new mysqli($hostname, $username, $password, $databaseName);
$fsearch="";

if(!empty($_POST['fsearch'])) {
$fsearch = $_POST['fsearch'];

$req = $connect->prepare("SELECT title FROM food_data_bg WHERE title LIKE ?");
$value = '%'.$fsearch.'%';
$req->bind_param("s", $value);
$req->execute();
$req->store_result();

if ($req->num_rows == 0){

echo 'Няма резултати';
}
else{
echo 'ДАА';
}
}
   

FINALY !!! that is the final result,it is printing ДАА when there is a result found and Няма резултати when there isn't any results fixed it after 1 month of pain lol Thanks to everyone which helped me <3<3 <3 <3 <3