2

I want to output like this

var data = { 
{
"cname": "Albania"
}
{
"cname": "Austria"
}
}

This code i tried but i am getting an error "Parse error: syntax error, unexpected T_VAR in C:\wamp\www\mvc\map\data.php on line 22"

<?php
header('Content-type: application/json');  // this is the magic that sets responseJSON

 $dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "test";

// Connecting, selecting database
$link = mysql_connect($dbhost, $dbuser, $dbpass)
    or die('Could not connect: ' . mysql_error());
mysql_select_db($dbname) or die('Could not select database');

        $query = "SELECT cname FROM country";       // Performing SQL query
        $result = mysql_query($query) or die('Query failed: ' . mysql_error());
        $all_recs = array();
        while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $all_recs[] = $line;
        }


var data = {

echo json_encode($all_recs);

};

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

can anyone guide me how to fix this,thanks

1
  • Where did you read that kind of syntax ?! Commented Apr 3, 2014 at 8:11

2 Answers 2

5

I'm not sure why you want that, but you can mix js code in php like this, you may try

$jsonData = json_encode($all_recs);
echo "var data = {$jsonData}";
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for understanding what the OP wanted ... i was clueless, thought about some weird kind of array ...
0
$data = "cname : Albania cname : Austria";

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.