I am able to connect to my database, as it says Connection Successful when I go to the page. The problem is that I'm still not able to run a query against my database and have the results display.
I've tried following the instructions that were given to me in my class and kept getting HTTP 500 Error. So, I started to search the internet and have my config file set up differently than what my instructor said and was able to get it to say that the connection is successful.
Here is the main file:
<?php
$configd = include('config2.php');
$conn = mysqli_connect($configd['dbhost'], $configd['dbuser'], $configd['dbpass'], $configd['dbname']);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql = "SELECT Last_Name, First_Name FROM sample";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Last Name: " . $row["Last_Name"]. "<br>";
echo "First Name: " . $row["First_Name"]. "<br><hr>"
}
} else {
echo "0 results";
}
$conn->close();
?>
Here is the config file:
<?php
return array(
dbhost => 'localhost',
dbuser => 'cdefauw',
dbpass => 'sherbert00',
dbname => 'sample',
);
?>
When this is run, it's supposed to show a query of all of the first and last names that are within the sample database I made on phpmyadmin. When I run it right now, all I get is HTTP Error 500. If I remove the if ($result->num_rows >0) statement, then it says connection successful.