PHP / MySql for javascript not working

There is something wrong with the way I print MySQL and javascript is not executing it. Here is an example.

square[1] = "asdfasdfadsf";

When I type " asdfasdfadsf" from my MySQL database, javascript is not working. However, if I just type "asdfasdfadsf" in static HTML, it runs fine. I tried as many PHP functions and chart transformations as possible. Please, help!

+3
source share
2 answers

I suggest json_encodea PHP function. In addition to printing the string correctly, it also avoids all dangerous characters.

square[1] = <?php echo json_encode($my_string); ?>;
+5
source

You need to add quotes around the row from the database.

// Add quotes around the call which prints the vale from PHP.
// this turns it into a JavaScript string.
square[1] = '<?php echo "asdfasdfadsf"; ?>';
//----------^^----------------------------^^

// Or...
square[1] = '<?php echo $row["value_from_your_db"]; ?>';

. json_encode() , - , , , .

+3

All Articles