Skip to content

Instantly share code, notes, and snippets.

@ElectApp
Last active March 18, 2019 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectApp/70849d05bc65e91ef20442f52778d9b9 to your computer and use it in GitHub Desktop.
Save ElectApp/70849d05bc65e91ef20442f52778d9b9 to your computer and use it in GitHub Desktop.
PHP script for reading table on MySQL database
<?php
$servername = "localhost"; //ใช้ localhost กรณี server อยู่บน online
$username = "พิมพ์ที่นี่"; //username ของ DirectAdmin/phpMyAdmin
$password = "พิมพ์ที่นี่"; //password ของ DirectAdmin/phpMyAdmin
$dbname = "test"; //ชื่อ database
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, password FROM user_login_test"; //"SELECT ชื่อคอลัมป์ 1, ชื่อคอลัมป์ 2, ชื่อคอลัมป์ 3 FROM ชื่อตาราง"
$result = $conn->query($sql);
$myArray = array();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($myArray, $row); //Add object to array
}
}
$json_array = json_encode($myArray); //Convert array to JSON
echo $json_array;
$conn->close(); //ปิดการเชื่อมต่อ
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment