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.
<?php if (!defined('DATABASE_HOST')) die("I forgot to include the config file"); ?>at the very top of your homePageLink.php