1

I am very new to PHP and Javascript. I would like to use a PHP function in order to fetch database data as an array and be able to use it in Javascript. I am I have searched online but I haven't come across anything that helps my particular situation. How would I get the array in Javascript and be able to use it like so:

var arrayJS = arrayFromPhp;

My PHP code is below:

<?php
    function fetch(){

        $link = mysqli_connect("localhost", "root", "Southflorida8", "test");
        if ($result = mysqli_query($link, "SELECT * FROM aircraft")){


            if (!mysqli_query($link, "SET @a:='this will not work'")) {
                    printf("Error: %s\n", mysqli_error($link));
            }

            $array = mysqli_fetch_all($result);
            mysqli_free_result($result);


            return json_encode($array);
        }
    }

    if (isset($_POST['fetch'])){

        echo fetch();
    }
?>

Here is the Javascript:

var array = [];



           $.ajax({
                url: 'server.php',
                type: 'post',
                data: 'fetch',
                datatype: 'json',
                success: function(array) {alert(array);}
            });
 pubnub.publish({channel:pnChannel, message:{lat:array[0][1] , lng:array[0][2] }});
8
  • 1
    Where does it fail? Do you get back JSON, does the AJAX request make it to the server? Commented Oct 24, 2017 at 3:35
  • 1
    I don't know PHP, so I can't help on the question itself, but I believe instead of "index" you mean the term "iterate." One iterates through an array. So searching with that term may return useful answers you could not find previously. Commented Oct 24, 2017 at 3:36
  • 1
    I think you get JSON in alert...? Commented Oct 24, 2017 at 3:38
  • 1
    For debug better use console.log than alert Commented Oct 24, 2017 at 3:39
  • 2
    @Flower If you are getting Uncaught ReferenceError you may not be including the jquery library, are you sure you have that included properly? Commented Oct 24, 2017 at 4:05

1 Answer 1

2

Try this.

    $.ajax({
        url: 'server.php',
        type: 'post',
        data: 'fetch',
        success: function(array) {
             array = JSON.parse(array);
             console.log(array);
        }
    });

**EDIT: ** And also update your php code.

Replace $array = mysqli_fetch_all($result); with $array = mysqli_fetch_all($result, MYSQLI_ASSOC);

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

8 Comments

I receive this error: Uncaught ReferenceError: $ is not defined at phpconnect.php:65 This line corresponds to "$.ajax({"
Can you comment out the array = JSON.parse(array) line. Then, tell me, what was written in the console?
Have you added the jquery js?
Yes, that fixed the previous problem. But I have a new error now: Uncaught TypeError: Cannot read property '1' of undefined. For some reason its not reading the array elements im trying to index
Have you now comment out the array = JSON.parse(array) line? We need to know what it returned.
|

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.