1

Below is my php file to apply query

    $host = "localhost";
    $user = "root";
    $pass = "abc123";
    $databaseName = "class";
    $con = mysql_connect($host,$user,$pass);
    $dbs = mysql_select_db($databaseName, $con);
    $result = mysql_query("SELECT id, name FROM lecturer");
    if (mysql_num_rows($result)) {
        $data = array();
        while ($row = mysql_fetch_assoc($result)) {
            $data[] = array(
               'id' => $row['id'],
               'name' => $row['name']
            );
        }
        header('Content-type: application/json');
        echo json_encode( $data );  
    }  

And this is my other file where i am applying javascript. I have searched a lot of same queries but could not find solution. Please help me

    <script type="text/javascript">
        function lecturer(){
            $("#a1_title").empty();
            $("#a1_title").append("<option>Default</option>");
            $.ajax({
                    type:'POST',
                    url : 'get-data.php',
                    contentType :"application/json; charset-utf8",
                    dataType:'json',
                    type:'POST',
                    success:function(data){
                        $('#a1_title').empty();
                        $('#a1_title').append("<option>Default</option>");
                        $.each(data, function(i, data){
                            $('#a1_title').append('<option   value="'+data[i].id+'">'+data[i].name+'</option>');
                        });
                    },
                    complete: function(){
                    }
            )};
        }
        $(document).ready(function(){
            lecturer();
        });
    </script>

Please help me I have tried to solve this problem buy i am not able to do it.

CodeGodie
  • 12,116
  • 6
  • 37
  • 66
Muhammad Umar
  • 349
  • 3
  • 14
  • 1
    If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Sep 09 '15 at 19:13
  • syntax error. when creating arrays, they should have parenthesis `$data[] = array()`, but in the code you provided, you have braces `$data[] = array{}`. However in your case, this: `$data[] = $row` should suffice. – CodeGodie Sep 09 '15 at 19:22
  • oops sorry....I have changed it but still its not working – Muhammad Umar Sep 09 '15 at 19:29
  • can you update your original question with the changes you made? – CodeGodie Sep 09 '15 at 19:29
  • your `$.ajax` does not have the correct closing curly brace `{}` in place. Check that. – CodeGodie Sep 09 '15 at 19:33
  • I just put parenthesis instead of braces. but if i am replacing array with row even then its not working while ( $row = mysql_fetch_assoc($result) ) { $data[] = $row; } – Muhammad Umar Sep 09 '15 at 19:37

1 Answers1

2

Your PHP code had bad syntax since you had your array() was surrounded with curly braces rather than parentheses. You also had some errors in your $.ajax, a misplaced parentheses. In addition, you do not need an iterator ([i]) in your $.each() function - you can just get each item's bit of information by associating this iteration's values. And as @Jay Blanchard said, mysqli would be used best here.

Try the following editions:

PHP:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$databaseName = "test";
$con = mysqli_connect($host, $user, $pass, $databaseName);
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT id, name FROM lecturer");
if (mysqli_num_rows($result)) {
    $data = array();
    while ($row = mysqli_fetch_assoc($result)) {
        $data[] = $row;
    }
    header('Content-type: application/json');
    echo json_encode($data);
}
?>

JS:

<script type="text/javascript">
    function lecturer() {
        $("#a1_title").empty();
        $("#a1_title").append("<option>Default</option>");
        $.ajax({
            type: 'POST',
            url: 'get-data.php',
            contentType: "application/json; charset-utf8",
            dataType: 'json',
            type: 'POST',
            success: function (data) {
                $('#a1_title').empty();
                $('#a1_title').append("<option>Default</option>");
                $.each(data, function (k, v) {
                    $('#a1_title').append('<option value="' + v.id + '">' + v.name + '</option>');
                });
            },
            complete: function () {
            }
        });

    }
    $(document).ready(function () {
        lecturer();
    });
</script>
CodeGodie
  • 12,116
  • 6
  • 37
  • 66