0

我有一个博客,我正在构建和扩展以获得 php 经验。我决定从 mysql 迁移到更新的 mysqli。我还创建了单独的文件来定义变量并连接到数据库。我有 appConfig.php 定义了我所有的变量和函数:

<?php
    define("DEBUG_MODE", true);
    define("SITE_ROOT", "http://blog.zacharysaathoff.com/");
    define("DATABASE_HOST", "127.0.0.1");
    define("DATABASE_USERNAME", "**********");
    define("DATABASE_PASSWORD", "**********");
    define("DATABASE_NAME", "**********");
    define("HOST_WWW_ROOT", "/home1/ohairclu/public_html/zsaathoffblog/");
    function js_redirect($url, $seconds=0) {  
        echo "<script language=\"JavaScript\">\n";  
        echo "<!-- hide from old browser\n\n";       
        echo "function redirect() {\n";  
        echo "window.location = \"" . $url . "\";\n";  
        echo "}\n\n";  
        echo "timer = setTimeout('redirect()', '" . ($seconds*1000) . "');\n\n";  
        echo "-->\n";  
        echo "</script>\n";  
        return true;  
    }  
    function handle_error($user_error_message, $system_error_message) {
        js_redirect('http://blog.zacharysaathoff.com/error.php?error_message='.$user_error_message.'&system_error_message='.$system_error_message, 0);
        exit();
    }
    function debug_print($message) {
        if (DEBUG_MODE) {
            echo $message;
        }
    }
    echo "configured";
?>

我还有 databaseConnection.php 应该为我连接到数据库:

<?php
    require_once 'appConfig.php';
    global $mysqli;
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
    if ($mysqli->connect_errno) {
        handle_error("There was a problem connecting to the database that holds the information we need to get you connected.", "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    return $mysqli;
?>

我知道全局变量是不可取的。这只是我尝试过的。与return. 我知道它可能不属于那里。我需要这些文件的文件是 homePageLinks.php:

<?php
    require_once 'appConfig.php';
    require_once 'databaseConnection.php';
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
    if ($mysqli->connect_errno) {
        handle_error("There was a problem connecting to the database that holds the information we need to get you connected.", "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
    $stmt = $mysqli->prepare("
        SELECT `date`, `title`, `path`, `image_path`
        FROM `ohairclu_blog_comments`.`posts` 
        ORDER BY `date` DESC LIMIT 8;
    ") or handle_error("There was a problem getting the recent posts.", "prepare failed :".htmlspecialchars($mysqli->error));
    $stmt->execute() or handle_error("There was a problem getting the recent posts.", "execute failed :".htmlspecialchars($stmt->error));
    $stmt->bind_result($date,$title,$path,$image_path) or handle_error("There was a problem getting the recent posts.", "bind_result failed :".htmlspecialchars($stmt->error));
    while($stmt->fetch()) {
        $formatted_date = date('F j, Y', strtotime($date));
        $results .= '<a href="'.$path.'" class="recent"><h4 class="home-head">'.$formatted_date.':<br>'.$title.'</h4><img src="'.$image_path.'" class="no-border"></a>';
    }
    echo $results;
?>

使用此配置,它可以工作。但是,我在 homePageLink.php 中重复了 databaseConnection.php 代码。但是每当我从 homePageLink.php 中删除这段代码时

$mysqli = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
if ($mysqli->connect_errno) {
    handle_error("There was a problem connecting to the database that holds the information we need to get you connected.", "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error);
}

这没用。当我删除它时,mysqli 准备失败。但是没有错误信息。我不知道为什么它不起作用。有趣的是,它在其他文件中按照我想要的方式工作。除了查询和输出之外没有任何区别。

4

1 回答 1

2

我不确定您为什么要global在 databaseConnection.php 中声明任何内容。你也用return错了。这两个语句都与函数一起使用,但该文件中没有函数。尝试删除这两个声明,看看是否有效。

<?php
    require_once 'appConfig.php';
    $mysqli = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);
    if ($mysqli->connect_errno) {
        handle_error("There was a problem connecting to the database that holds the information we need to get you connected.", "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error);
    }
于 2013-10-31T16:32:32.997 回答