Search code examples
phpmysqliinternal-server-error

500 Internal Server Error, but works fine on localhost - $mysqli = new mysqli


I'm trying to download a csv file getting contents from database, but I want to get it's name from the database as well from a single table (filename) with a single field (name).

Here's my code:

// Connecting to Database

$mysqli = new mysqli('servername.ipagemysql.com', 'username', 'pass', 'reportconverter');
$filename = array_values(mysqli_fetch_array($mysqli->query("SELECT name FROM filename")))[0]; 

//Downloading the file

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

echo $output;

exit;

I get the following error:

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

The servername, username, password and DB name are all accurate, because they are working on other pages. The code also works on local host. But if I put server login details in the localhost file, I get this error:

Warning: mysqli_connect(): (HY000/2002): No connection could be made because the target machine actively refused it. 

Any ideas? Or shall I ask my hosting service for help?


Solution

  • Finally I fixed the problem. It was the [0] causing the issue, because the php version on my server doesn't support that statement, so I had to change it like this:

    $filename = array_values(mysqli_fetch_array($mysqli->query("SELECT name FROM filename"))); 
    $filename = $filename[0];