I'm currently working on an Android app that needs to get and send data via PHP to a server with a MySQL database.
I'm relatively new to PHP and I couldn't find any specific help for my problem.
Here is my code
<?php
$db_name = "testwz";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
$sql = "SELECT verbrauchsstelle.ID, adressen.Ort, adressen.Ortsteil,
adressen.PLZ, adressen.Strasse, adressen.Hausnummer, zaehler.Nummer
FROM verbrauchsstelle
LEFT JOIN adressen ON verbrauchsstelle.adressen_id = adressen.ID
LEFT JOIN zaehler ON zaehler.Verbrauchsstelle_id = verbrauchsstelle.ID;
";
$result = mysqli_query($con, $sql);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response, array(
"vID" => $row[0],
"Ort" => $row[1],
"Ortsteil" => $row[2],
"PLZ" => $row[3],
"Strasse" => $row[4],
"Hausnummer" => $row[5],
"zNr" => $row[6]
));
}
echo json_encode(array("server_response" => $response));
mysqli_close($con);
?>
The SQL itself is doing fine when used with SQLyog; when I delete one of the LEFT JOINs I get an output but using both of them, I get a blank white screen in the browser. I tried LEFT JOIN and INNER JOIN with the same result.
Maybe someone with way better PHP knowledge can help me find the error.
Thanks in advance.
You can try this:
<?php
$db_name = "testwz";
$mysql_user = "root";
$mysql_pass = "";
$server_name = "localhost";
$con = mysqli_connect($server_name, $mysql_user, $mysql_pass, $db_name);
$sql = "SELECT verbrauchsstelle.ID, adressen.Ort, adressen.Ortsteil,
adressen.PLZ, adressen.Strasse, adressen.Hausnummer, zaehler.Nummer
FROM verbrauchsstelle
LEFT JOIN adressen ON verbrauchsstelle.adressen_id = adressen.ID
LEFT JOIN zaehler ON zaehler.Verbrauchsstelle_id = verbrauchsstelle.ID
";
$result = mysqli_query($con, $sql);
if (!mysqli_error($con)) {
echo json_encode(array("server_response" => mysqli_error($con)));
mysqli_close($con);
exit;
}
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response, array(
"vID" => $row[0],
"Ort" => $row[1],
"Ortsteil" => $row[2],
"PLZ" => $row[3],
"Strasse" => $row[4],
"Hausnummer" => $row[5],
"zNr" => $row[6]
));
}
echo json_encode(array("server_response" => $response));
mysqli_close($con);
This will show you the exact error if any else it will give you the desired output what you want.