Good afternoon. I'm teaching myself SQL and PHP, and have run into a roadblock. I imagine it's quite simple but of course I can't figure it out. I have a server running Apache2, MySQL, and PHP8.1. I need to host a local webpage that lists DB table information using the PDO method. I've written the HTML and PHP, but when running the PHP script within the HTML page, I get a blank space, and if I run the PHP script on its own, I get a 500 error. After looking at Apache logs, I've found that it's a 500 185 error, which I have learned is essentially a "too much to handle" error (but it's not). Here's my code:
$servername = "[local IP:port]";
$username = "[sql username]";
$password = "[sql password]";
$databasename = "labeling";
// CREATE CONNECTION
$conn = mysqli_connect($servername,
$username, $password, $databasename);
// GET CONNECTION ERRORS
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// SQL QUERY
$query = "SELECT partnum, partdesc, colorant, material, quantity, monum, boxnum FROM `press01`";
try
{
$conn = new PDO(
"mysql:host=$servername;dbname=$databasename",
$username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare($query);
// EXECUTING THE QUERY
$stmt->execute();
$r = $stmt->setFetchMode(PDO::FETCH_ASSOC);
// FETCHING DATA FROM DATABASE
$result = $stmt->fetchAll();
// OUTPUT DATA OF EACH ROW
foreach ($result as $row)
{
echo "Part #: " . $row["partnum"]. "Part Description: " .
$row["partdesc"]. "Color: " . $row["colorant"]. "Material: " .
$row["material"]. "Box Quantity: " . $row["quantity"]. "MO#: " . $row ["monum"].
"Box #: " . $row["boxnum"]. "<br>";
}
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$conn->close();
?>
I expect to be given a list with the aforementioned values. What am I doing wrong? Forgive me, I'm still green with PHP.
After enabling error reporting, I'm met with the following:
Fatal error: Uncaught Error: Call to undefined method PDO::close() in /var/www/html/press01.php:55 Stack trace: #0 {main} thrown in /var/www/html/press01.php on line 55