0

我试图通过仅包含哪些将具有我需要的变量/值来避免硬编码。conf.php

我怎样才能做到这一点?

这是我的conf.php代码

<?php
mysql_connect("localhost", "tiger","tiger") or die (mysql_error ());
mysql_select_db("theaterdb") or die(mysql_error());
?>

其他PHP页面的编码如下:

$q = strtolower(trim($_GET["q"]));

try {
    $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

$sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q';

$sth = $dbh->prepare($sql);
$sth->bindValue(':q', $q);
$sth->execute();

而不是这一行:

dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');

我想使用该conf.php文件。我将如何做到这一点?

4

1 回答 1

0

//在所有其他页面中包含conf.php

配置文件

<?php
    try {
        $dbh = new PDO('mysql:dbname=theaterdb;host=localhost','tiger','tiger');
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
?>

other_page.php

<?php
    include("conf.php");

    $q = strtolower(trim($_GET["q"]));

    $sql = 'SELECT address FROM theater WHERE LOWER(theater_name) = :q';

    $sth = $dbh->prepare($sql);
    $sth->bindValue(':q', $q);
    $sth->execute();

?>
于 2013-09-05T04:50:53.143 回答