简体   繁体   中英

Can't fetch data from database table… 500 error

I have tried a ton of different versions of this code, from tons of different websites. I am entirely confused why this isn't working. Even copy and pasted code wont work. I am fairly new to PHP and MySQL, but have done a decent amount of HTML, CSS, and JS so I am not super new to code in general, but I am still a beginner

Here is what I have. I am trying to fetch data from a database to compare it to user entered data from the last page (essentially a login thing). I haven't even gotten to the comparison part yet because I can't fetch information, all I am getting is a 500 error code in chrome's debug window. I am completely clueless on this because everything I have read says this should be completely fine.

I'm completely worn out from this, it's been frustrating me to no end. Hopefully someone here can help. For the record, it connects just fine, its the minute I try to use the $sql variable that everything falls apart. I'm doing this on Godaddy hosting, if that means anything.

<?php
$servername = "localhost";
$username = "joemama198";
$pass = "Password";
$dbname = "EmployeeTimesheet";

// Create connection
$conn = mysqli_conect($servername, $username, $pass, $dbname);
// Check connection
if (mysqli_connect_errno) {
    echo "Failed to connect to MySQL: " . mysqi_connect_error();
}

$sql = 'SELECT Name FROM Employee List'; 
$result = mysqli_query($conn, $sql);

     if (mysqli_num_rows($result) > 0) {
        while($row = mysqli_fetch_assoc($result)) {
           echo "Name: " . $row["Name"]. "<br>";
        }
     } else {
        echo "0 results";
     }
     mysqli_close($conn);

?>

There be trouble here:

// Create connection
$conn = mysqli_conect($servername, $username, $pass, $dbname);
// Check connection
if (mysqli_connect_errno) {
    echo "Failed to connect to MySQL: " . mysqi_connect_error();
}

There are three problems here:

  1. mysqli_conect() instead of mysqli_connect() (note the double n in connect )
  2. mysqli_connect_errno should be a function: mysqli_connect_errno()
  3. mysqi_connect_error() instead of mysqli_connect_error() (note the l in mysqli )

The reason you're getting a 500 error is that you do not have debugging enabled. Please add the following to the very top of your script:

ini_set('display_errors', 'on');
error_reporting(E_ALL);

That should prevent a not-so-useful 500 error from appearing, and should instead show the actual reason for any other errors.

There might be a problem here:

$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {

If the query fails, $result will be false and you will get an error on the mysqli_num_rows() call. You should add a check between there:

$result = mysqli_query($conn, $sql);

if (!$result) {
    die('Query failed because: ' . mysqli_error($conn));
}

if (mysqli_num_rows($result) > 0) {

The name of your database table in your select statement has a space in it. If that is intended try:

$sql = 'SELECT Name FROM `Employee List`'; 

i think you left blank space in your query.

$sql = 'SELECT Name FROM Employee List';

change to

$sql = "SELECT `Name` FROM `EmployeeList`";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM