0

I'm a beginner in using PHP and Javascript, and I don't have any idea on how to store the data that I've gathered from MySQL which I placed in a multidimensional array in PHP to a 2D array in Javascript. Here's my working code in PHP:

<?php

function connecToDatabase(){
$host = "localhost";
$username = "root";
$password = "p@ssword";
$database = "flood_reports";

mysql_connect("$host", "$username", "$password") or die(mysql_error());

mysql_select_db("$database") or die(mysql_error());
}

function retrieveData(){
connecToDatabase();
$data = mysql_query('SELECT * FROM entries') or die(mysql_error());
$entries = array(); 
$index = 0;
while($info = mysql_fetch_array( $data )) 
{ 
    $entries[$index] = array('entry_id' => $info['entry_id'], 
    'location' => $info['location'], 
    'image_dir' => $info['image_dir'], 
    'longitude' => $info['longitude'], 
    'latitude' => $info['latitude'], 
    'level' => $info['level']);
    $index++;
}

$json = json_encode($entries);
echo $json;

mysql_close();
}

retrieveData();

?>
1
  • How is this PHP being executed, is it part of an entire HTML page or is this script requested via AJAX/XHTP? Commented Apr 6, 2014 at 8:57

2 Answers 2

1

on the end of your script add the following

   <script type="text/javascript">
          var jsvar = <?php echo $phpvar;?>
   </script>
Sign up to request clarification or add additional context in comments.

Comments

0

Replace

echo $json;

with

echo 'var fromPhp = ' . $json . ';';

You just need to put the data into a variable. This will make it available as fromPhp on the browser side.

Comments

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.