Sending MySQL DB data to JavaScript using PHP

Sending MySQL DB data to JS with help of PHP

Steps :
1. MySQL Connection using PHP (below PHP code is saved as connect.php)

$host = “localhost”;
$user = “root” ;
$password = “”;
if(!$con){die(“Failed to connect”);}

2. Retrieve DB data to PHP array.

$sql=”select * from db_table_name”;
foreach($result as $row){

3) (i) Assign PHP value to JS variable
var db_data = <?php echo json_encode($array , JSON_PRETTY_PRINT); ?>;
//result maybe 0001.

3) (ii) Assign PHP value to AngularJS variable (More easy one)
<div ng-app=””>
<div ng-init=”db_data== <?php echo json_encode($array , JSON_PRETTY_PRINT); ?>” >
<select ng-options=”y.db_colum_name for (x,y) in db_data” >
</select> // Create Select box with db data

Tagged , ,

Leave a Reply