fetch mysql data in php array code example
Example: print sql query result as an array php
<?php
$connection = mysqli_connect( 'localhost', 'root', 'dbPass' );
if ( !$connection ) {
die( 'Database Connection Failed' . mysqli_error( $connection ) );
}
$select_db = mysqli_select_db( $connection, 'sample_login' );
if ( !$select_db ) {
die( 'Database Selection Failed' . mysqli_error( $connection ) );
}
$query = 'SELECT * FROM login';
$result = mysqli_query( $connection, $query );
$stack = array();
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ) {
array_push( $stack, $row );
}
$theArray = json_encode( $stack );
print_r( $theArray );
?>
<script>
var theArray = <?php echo $theArray ?> ;
console.log(theArray);
</script>