2

私の状況:フォルダ構造は/ resources / website / inc /です(/ resources /は設定が保存される場所であり、/website/はindex.phpと/inc/を保持します

config.php/resourcesフォルダーでdb接続の定数を定義しました。

   define ( 'DB_HOST', 'localhost' );
   define ( 'DB_USER', 'my_username' );
   define ( 'DB_PASSWORD', 'my_password' );
   define ( 'DB_NAME', 'my_database' );

index.phpでは、データベースにアクセスしてデータを表示します。

config.phpで定義された定数をindex.phpのconnectステートメントで使用しようとしているという結論に達しましたが、定数が渡されていません。

これは私の接続ステートメントです:

$connect = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or 
                         die("MySQL Error: " . mysql_error());
$db = mysql_select_db(DB_NAME) or die("MySQL Error: " . mysql_error());

両方の部分をconfig.phpに追加すると、完全に機能しますが、さまざまな領域で接続と接続を閉じたいので、変数を再度定義する必要はありません。

4

1 に答える 1

5

Web サーバー経由でアクセスできる他のすべてのファイルに構成ファイルを要求する (含める) 必要があります。

次の行だけで何か:

config.php

define ( 'DB_HOST', 'localhost' );
define ( 'DB_USER', 'my_username' );
define ( 'DB_PASSWORD', 'my_password' );
define ( 'DB_NAME', 'my_database' );

public/index.php

require_once __DIR__ . '/../config.php';
// The rest of you code here

public/contact.php

require_once __DIR__ . '/../config.php';
// The rest of you code here
于 2012-05-03T01:34:57.857 に答える