Last active
March 18, 2019 03:48
-
-
Save ElectApp/70849d05bc65e91ef20442f52778d9b9 to your computer and use it in GitHub Desktop.
PHP script for reading table on MySQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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