Connor Thomas
Connor Thomas

Reputation: 59

Installing MySQLi on AWS server

I had my database working locally on my computer, but have been spending the last few hours trying to figure out AWS permissions and servers. I finally tracked down my issue with my site to the mysqli connection function. Any ideas on how I can fix this? I am receiving the error:

Failed to load resource: the server responded with a status of 500 ()

Which isn't super helpful error code. Then my code is this (keep in mind I removed my personal information from the code to access the database.)

<?php
header("Access-Control-Allow-Origin: http://bucketsiteurl");
header('Access-Control-Allow-Credentials: true');
header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Content-Type: application/json; charset=UTF-8");


$db_host = 'databaseurl:3306';
$db_username = 'user';
$db_password = 'password'; 
$db_name = 'dbname';
$mysqli = new mysqli($db_host, $db_username, $db_password,$db_name);

if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}

define('post_host', 'databaseurl:3306');
define('post_user', 'user');
define('post_pass', 'password');
define('post_name', 'dbname');

function connect()
{
  $connect = new mysqli(post_host ,post_user ,post_pass ,post_name);

  if (mysqli_connect_errno($connect)) {
    die("Failed to connect:" . mysqli_connect_error());
  }

  mysqli_set_charset($connect, "utf8");

  return $connect;
}

$con = connect();
?>

My php script works perfectly when I host it on my computer internally and I go to the script location, but when I try to run it on the AWS server I receive the error above. I know the server is working in general because I did some other php scripts that just has

<?php
echo phpinfo();
?>

And this script executed just fine showing the php information. My other scripts worked as well with the header information correctly. The only script that I am having issues with is my database php script that connects to my MySQL server. Any suggestions are appreciated, thanks!

UPDATE: I am using PuTTY to access the php scripts.

[Mon Apr 12 05:17:46.467711 2021] [:error] [pid 1067] [client ip] PHP Fatal error:  Class 'mysqli' not found in /var/www/html/database.php on line 13

Upvotes: 2

Views: 510

Answers (1)

Felix
Felix

Reputation: 10078

you need to install (yum or apt, depending on your OS) php-mysqli. When you do, your phpinfo() will have mysqli section The problem has nothing to do with AWS, by the way.

Upvotes: 2

Related Questions