0

I need to show data retrieved from mysql in a page. My codes are :

jquery:

$('#search').on('click', function() { 
        alert($('.search_word').val());
        var data_to_php = $('.search_name:visible').val(); //take the text only from the visible search box
        alert(data_to_php);


        //////////////////////////////////////////////////////////////////////
        //The following code will get the search list populated  by //////////
        //retrieving data from mysql table////////////////////////////////////
        //////////////////////////////////////////////////////////////////////

        $.getJSON('./search.php', 'keyword='+data_to_php, function(info) {

                $.each(info, function() {
                    $('ul.search_list').append('<li   class="search_data">hi</li>'); //this code is for the purpose of testing only

                    console.log(info);
                }); 


        });

    }); //end #search.on.click

the php code is :

require(connect.php);

$keywords = $_GET['keyword'];

$query = "SELECT name FROM test ORDER BY name WHERE name LIKE   '%".$_GET['keyword']."%'";

$result = mysqli_query($conn,$query); //connection variable
                                      //$conn is from
                                      //included file
                                      //connect.php 

$jsonData = array();

while($row = mysqli_fetch_array($result)){

    $jsonData[] = $row;

}

echo json_encode($jsonData);
?>

In console.log output, I am getting :

GET ./search.php?keyword=s 500 Internal Server Error with blank response. Internal error 500 can as well be generated if php is not working properly. There might also be some error in the code.

Now phpinfo is showing results properly.

How do I know if the getjson call to php script is successful?

4
  • What happens when you try to access ./search.php?keyword=s just in the browser? Do you still get error 500? Commented Feb 26, 2016 at 18:42
  • yes - with server error 500, it is showing : The website encountered an error while retrieving localhost/autocomplete/search.php?keyword=s. It may be down for maintenance or configured incorrectly. Commented Feb 26, 2016 at 18:44
  • what do you get on var_dump($jsonData); ?? Commented Feb 26, 2016 at 18:44
  • how do I see the content of var_dump? Commented Feb 26, 2016 at 18:48

1 Answer 1

3

There is an error in your MySQL query, you cannot call ORDER BY before WHERE clause, modify your query,

SELECT name FROM test WHERE name LIKE '%".$_GET['keyword']."%' ORDER BY name

A very good reference to know the correct order of execution in MySQL - MySQL query / clause execution order

Also, your file name in the require function should be enclosed within quotes - require('connect.php');

Sign up to request clarification or add additional context in comments.

5 Comments

Thnk you for pointing out the error. But unfortunately the same error is showing up ....!! I did not forget to restart apache though ....
Did you check your Mysql connection ? what do you have in connect.php ?
it is : define('DB_SERVER', "localhost"); define('DB_USER', "root"); define('DB_PASSWORD', ""); define('DB_TABLE', "test_php"); $conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_TABLE); if ($conn->connect_error) { trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR); } but connection error does not show either
that looks alright, hope you have the credentials right though...since you're getting a 500 error it has to be on your Server side,
I think your require is causing the problem, modify it like this require("connect.php"); it should be enclosed within quotes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.