0

I have a blog that I am building and expanding to gain experience in php. I decided to move from mysql to the newer mysqli. I also created separate files that define variables and connect to the database. I have appConfig.php that defines all my variables and functions:

<?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";
?>

I also have databaseConnection.php which should connect to the database for me:

<?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;
?>

I know global variables are not desirable. It was just something I tried. Same thing with the return. I know it probably doesn't belong there. The file I require these files in is 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;
?>

With this configuration, it works. However, I am repeating the databaseConnection.php code in the homePageLink.php. But whenever I remove this code from 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);
}

it does not work. The mysqli prepare fails when I remove it. But there is no error message. I can not figure out why it does not work. The funny thing is that it is working the way I want it to in other files. There is no difference aside from the queries and outputs.

1
  • add a test <?php if (!defined('DATABASE_HOST')) die("I forgot to include the config file"); ?> at the very top of your homePageLink.php Commented Oct 31, 2013 at 16:36

1 Answer 1

1

I'm not sure why you're declaring anything global in databaseConnection.php. You also used return incorrectly. Both statements are used in conjunction with functions but there is no function in that file. Try removing both declarations and see if that works.

<?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);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

That's what I thought, but it was not working without them, so I decided to try it. I'll remove them again and see what happens, but it wasn't working before.
Ok, I tried it. It works now. I have no clue why it would not earlier, but that's in the past now. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.