ここで超混乱。PHPを使用してmysqlに接続できるWebサイトを構築しました。以下に示すように、common.php が必要ですが、データベースへの接続、実行などの問題はありません。うまくいかないのは、開発中の新しいページでこのまったく同じコードを使用する場合です。テスト ユーザーでサイトにログインできますが、ページで SQL クエリを実行してデータベースから情報を取得することはできません。私が得るエラーは次のとおりです。
「ユーザー 'ec2-user@localhost' のアクセスが拒否されました (パスワードを使用: NO)」
コードに接続を求めているのは明らかにそうではないのに、なぜec2-userとして接続しようとするのか理解できません。
どんな助けでも大歓迎です。数日前からこの問題を解決しようとしていますが、Father Google で検索しても適切な答えが見つかりません。
ありがとう!
Common.php コード
// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
// If they are not, we redirect them to the login page.
header("Location: index.php");
// Remember that this die statement is absolutely critical. Without it,
// people can view your members-only content without logging in.
die("Redirecting to index.php");
}
common.php のコードは以下のとおりです
<?php
// These variables define the connection information for your MySQL database
$username = "username";
$password = "password";
$host = "myhostnotyours";
$dbname = "thedatabase";
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try
{
$db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect to the database: " . $ex->getMessage());
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
function undo_magic_quotes_gpc(&$array)
{
foreach($array as &$value)
{
if(is_array($value))
{
undo_magic_quotes_gpc($value);
}
else
{
$value = stripslashes($value);
}
}
}
undo_magic_quotes_gpc($_POST);
undo_magic_quotes_gpc($_GET);
undo_magic_quotes_gpc($_COOKIE);
}
header('Content-Type: text/html; charset=utf-8');
session_start();