わかりましたので、mysqlよりもはるかに安全だと聞いたので、phpで準備されたステートメントを学習しています
だから今、関数を手に入れただけです。mysql dbに接続する関数を作成し、それを使用したいページとは別のフォルダーに配置しました。だから私はすべての主要な機能を1つのファイルに入れたいので、必要なページのファイルからそれらを呼び出したいと思っています。しかし今、phpはそれを好まない.
これが私の functions.php ページです
function mysqlconnect(){
$host = 'localhost';
$port = 3306; // This is the default port for MySQL
$database = '';
$username = '';
$password = '';
// Construct the DSN, or "Data Source Name". Really, it's just a fancy name
// for a string that says what type of server we're connecting to, and how
// to connect to it. As long as the above is filled out, this line is all
// you need :)
$dsn = "mysql:host=$host;port=$port;dbname=$database";
// Connect!
$db = new PDO($dsn, $username, $password);
}
そして、これが私のテストページで、関数の呼び出しをテストしています。
include 'functions/functions.php';
mysqlconnect();
$_POST['fish'] = "shadow" ;
$statement = $db->prepare("SELECT * FROM users WHERE username = ?");
$statement->execute(array($_POST['fish']));
while ($result = $statement->fetchObject()) {
echo $result->username;
echo "<br />";
}
ファイルをインクルードして関数を呼び出すことに注意してください。
Notice: 未定義の変数: db 致命的なエラー: オブジェクト以外でメンバ関数 prepare() を呼び出しています
接続を同じphpファイルに入れると、すべて正常に動作します。もちろん、同じファイル内のすべての関数が好きで、必要なときに呼び出すだけです。私は何を間違っていますか??