0

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 ?

2 Answers 2

3

Never ever ever output PHP values directly.

output(<?php echo json_encode($str) ?>);
Sign up to request clarification or add additional context in comments.

3 Comments

@OM: It makes sure that the PHP value is converted to a valid JavaScript literal, since all JSON strings are valid JavaScript literals.
+1 Oh Great can you please suggest me some good learning resource on this?
0

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

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.