모바일/Android

Mysql -> PHP -> JSON select시 코드

늘근이 2016. 5. 10. 22:08

<?

 $con=mysqli_connect( "주소", "계정"비번","db or die( "cannot connect to MySQL Server");
 
 
if (mysqli_connect_errno($con)) 

   echo "Failed to connect to MySQL: " . mysqli_connect_error(); 

$sql = "SELECT * FROM 테이블";

$result = $con->query($sql);
$return_array = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        array_push($return_array, $row);
    }
} else {
    echo "0 results";
}

echo json_encode($return_array);

mysql_close($con);

?>