简体   繁体   中英

Connecting to AWS RDS database using beanstalk and PHP

In PHP 7, As I try to connect to the Beanstalk I have set up, I receive the error:

Fatal error: Uncaught Error: Class 'mysql' not found in C:\\Apache24\\htdocs\\php_file.php, Stack trace: #0 {main} thrown in C:\\Apache24\\htdocs\\php_file.php on line 41.

This server uses AWS RDS running MYSQL, and I am using apache 2.4 to localhost (for testing).

The code I'm using is:

$servername = "MY BEANSTALK CONNECTION";
$username = "Username";
$password = "PSSWD";
$dbname = "DBNAME";

$conn = new mysql($servername, $username, $password, $dbname);

$sql = "SELECT * FROM column";
$result = $conn->query($sql);

$conn->close();

My updated code uses mysqli, but is still getting the same error.

From PHP documentation mysql class is deprecated since version 5.5 and removed in version 7. that's why you're receiving the class not found error.

Try using mysqli . The PHP documentation has good examples on this. Look at example 2. I've pasted part of the example in this post for you. https://www.php.net/manual/en/function.mysql-connect.php

<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
}

$res = $mysqli->query("SELECT 'choices to please everybody.' AS _msg FROM DUAL");
$row = $res->fetch_assoc();
echo $row['_msg'];
?>

改用mysql_connect($ servername,$ username,$ password)。

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