I am trying to create a simple PHP script that selects data using prepared statements. Currently, I am using this tutorial as a source. When I call my script, I have no errors in the error.log, but no data is displayed. Here is my script:
<?php
$servername = "xxx";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("SELECT * FROM Regions WHERE id=?");
$stmt->bind_param("i", $_POST['id']);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$name[] = $row['name'];
$center[] = $row['center'];
$content_string[] = $row['content_string'];
}
}
$stmt->close();
echo $name;
echo $center;
echo $content_string;
$conn->close();
?>
My structure looks like this:
I have tried to access the output by calling
http://URL/php/protected.php?id=1 and http://URL/php/protected.php to no avail. I've narrowed down that the error is happening because if($result->num_rows > 0)
never gets evaluated. From that, I figure that the error must be in my SQL statement.