MySQL:Select LIKE doesn't work when nothing found

+1 Yannis Sp · September 1, 2014
I have this code that searches on a DB if a name exists:

   
<?php
    $name = $_POST['name'];

    $get_search_results_q= "SELECT * FROM `users`.`info` WHERE `info`.`fullname` LIKE '%$name%' ";
    $get_search_results_run=mysql_query($get_search_results_q); 
    if($get_search_results_run==0){
    echo "No results were found";
    }else{
    while($rows = mysql_fetch_assoc($get_search_results_run,MYSQL_NUM)){
    $com_array[] = $rows[3];
    }
    echo json_encode($com_array);
    }
    
    ?>



It works fine when i type a name that exists in DB.
But when i type something that doesn't exist nothing is returned.
This is the jQuery AJAX code that displays the data:

   
.done(function(data) {
    var result = eval(data);
    if(!result){
    $(".results").empty();
    $(".results").append('<div class="horizontal result_element">'+result+'</div>');
    $("#search_result_box").show();
    }else{
    $(".results").empty();
    for (var i = 0; i < result.length; i++) {
       $(".results").append('<div class="horizontal result_element"><div class="result_photo"></div><div class="result_data">'+result+'</div></div>');
    }
    $("#search_result_box").show();
    }
    });



I tried many things,like to display the data on jQuery and the mysql_query result but nothing happens.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Yannis Sp · September 2, 2014
Great now i know :)
Thanks bro :)
+3 Samuel Oloruntoba · September 2, 2014
I see a lot pros, both API's are easy to use, very good, secure and fast, but I'd use PDO, because it allows for connection to different databases, say you change your database from mysql to postgresql, you don't need to change the rest of the code in your project, all you need to do is change the connection string, but with mysqli you would need to go through each file in your project that connects to a database and edit it.
+1 Yannis Sp · September 2, 2014
Samuel thanks for the reply but i found the solution yesterday.The problem in that situations (on Ajax callings) is that the return(s) should be one kind.In that example both should be json object.
p.s i know that mysql is old and no secure.Which method would you advise me to learn?PDO or MySQLi?I want the easier one to learn :P Please tell me the goods and bads of both.
+2 Samuel Oloruntoba · September 2, 2014
Try this
<?php

$name = mysql_real_escape_string($_POST['name']);

$query = "SELECT * FROM `users`.`info` WHERE `info`.`fullname` LIKE '%$name%' ";
$result = mysql_query($query);

if( mysql_num_rows($result) ){

header( 'Content-Type: application/json' );

while( $rows = mysql_fetch_assoc($result) ){
$com_array[] = $rows['name_of_table_column'];
}

echo json_encode($com_array);

} else {

echo "No results were found";

}

?>

this should work, but I should advice against using this method to connect to your database because it is deprecated, you should use DAO like PDO, mysqli.
  • 1

SQL & Databases

107,055 followers
About

Everything SQL and Databases related in here!

Links
Moderators
Bucky Roberts Administrator