Hai Truong IT
Hai Truong IT

Reputation: 4187

Error unterminated string literal when using php javascript?

I have a file with format

<script src="test.php"></script>

And code in test.php with table test(id, name) with 4000 rows

<?php
$sql = "Select * From test";
$data = array();
$query = mysql_query($sql);
while($row = mysql_fetch_object($query)) {
     $data[] = $row;
}
$str = '';
foreach($data as $row) {
    $str .= $row->name;
}
?>
output('<?php echo $str ?>');

When I run code is error is unterminated string literal, but when repair code with $sql = 'Select * From test order by id limit 100' is no error, how to fix it ?

Upvotes: 0

Views: 1112

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

Never ever ever output PHP values directly.

output(<?php echo json_encode($str) ?>);

Upvotes: 3

OM The Eternity
OM The Eternity

Reputation: 16204

You have not closed the script tag properly it has to be like </script>

Upvotes: 0

Related Questions