I am trying to Select everything from two tables and display them through JSON. Here is my shot at trying that:
<?php
// Create connection
$conn = new mysqli("localhost", "root", "****", "user");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Getting the received JSON into $json variable.
$json = file_get_contents('php://input');
// decoding the received JSON and store into $obj variable.
$obj = json_decode($json,true);
// Populate Username from JSON $obj array and store into $usnername.
$username = $_GET['username'];
$sql = "SELECT * FROM users WHERE username = '$username'";
$usql = "SELECT * FROM user_images WHERE username = '$username'";
$result = $conn->query($sql, $usql);
if ($result->num_rows >0) {
while($row[] = $result->fetch_assoc()) {
$tem = $row;
$json = json_encode($tem);
}
} else {
echo "No Results Found.";
}
echo $json;
$conn->close();
?>
I am not too sure if this is the correct way to achieve my task, but I looked at other questions and none are in the same format as mine. Also, I know this is vulnerable to SQL injections, this is just for example purposes.
users Table:
-id -username -profilepic
1 Bill image.png
2 Sally cats.png
user_images table:
-id -username -posts
1 Bill Food
2 Bill Sports
3 Sally Coffee