PHP - include_once/include 不工作
PHP - include_once/include not working
我正在为一个学校项目开发一个非常简单的 CMS 系统,但遇到了一些麻烦。
现在我的网页是这样设置的:
- 我创建的用户可以访问的每个网页 - 在顶部和底部,我有一个 include_once(header.php) 和 include_once(footer.php)分别。这些包含应该加载的任何页面的顶部和底部。
在我的 header.php 我有这个,还有其他杂项 HTML:
<?php
require_once('php/constants.php');
require_once("php/functions.php");
?>
对于 constants.php - 包含我能想到的每个已定义常量,因为我宁愿在此处更改一个常量,也不愿在我的代码中搜索以查找我在其他地方可能拥有的任何实例。
For Functions.php - 包含我为在该 CMS 上使用而创建的所有函数。包含从我使用 PHPMailer 的电子邮件功能到使用 MeekroDB 进行数据库交互的注册和登录的所有内容。
问题就出在这里。
在我的帐户注册页面上,有类似这样的内容
//get the username, password, first/last name, and send it to register function.
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$password = $_POST['password'];
$email_address = $_POST['email_address'];
//register the user. where register() is in functions.php
if(register($email_address, $password, $firstName, $lastName))
{
echo "REGISTRATION SUCCESSFUL";
}
else
{
echo "REGISTRATION UNSUCCESSFUL";
}
在我的 functions.php 文件中,我使用 MeekroDB 提到我使用的 DB::QUERY 赋值来声明我的数据库信息。
//MeekroDB - Used for database CRUDding
require_once("php/meekrodb.php");
DB::$user = 'root'; //yes i'll change this later
DB::$password = ''; //this too
DB::$dbName ="testDB";
在实际的寄存器函数中我有这个:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
//if there is a row in the DB with this email...
if(DB::count() >= 1)
{
return false;
}
else
{
//lets prepare the user for insertion into the database.
$username = $firstName . '_' . $lastName;
//generate a salt for database insertion and password hashing
$salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
$password = hash('sha512', $password . $salt);
//create the insert statement array for insertion.
$insertionArray =
array(
'username' => $username,
'password' => $password,
'email' => $email,
'salt' => $salt,
'isAdmin' => 0
);
//insert the new user into the database.
DB::INSERT('users', $insertionArray);
//Send a registration email to the new user.
//Send an email to the user stating they've registered.
$mailer = createMailer();
//set the contents of the email
$mailer->MsgHTML(generateEmail_Registration($username));
//set the destination address
$mailer->AddAddress($email, $firstName . ' ' . $lastName);
if($mailer->Send())
{
return true;
}
else
{
return false;
}
}
}
对我来说,这应该可行。
但是:
每当我尝试 运行 register() 函数时,我都会遇到大量涉及 MeekroDB 的错误,而且我不是 100% 确定如何修复它们(无论是 INCLUDE 问题,还是可能是meekroDB 的东西?)
这是我遇到的错误。
Warning: mysqli::set_charset(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 186
Warning: MeekroDB::get(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 188
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
REGISTRATION UNSUCCESSFUL //of course.
我认为这与我包含各种 PHP 文件的方式有关,并且在 return 中以某种方式扰乱了我的 MeekroDB 连接。
如果你们需要更多信息,请告诉我,但我认为我已经在 table.
上发布了所有内容
我不常post来这里,所以如果我在某处错过了某种礼仪,我提前道歉。
谢谢! :)
如果您按照 meekrodb.php 文件进行操作,第一个错误是在您刚刚尝试连接的 $mysql 对象上调用的(查看 meekrodb.php 文件看看我在说什么)所以这些错误可能与连接信息错误或连接到数据库的其他问题有关,因此其他功能失败。
查看更多信息性错误的一种简单方法是尝试使用他们的 $throw_exception_on_error
函数:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
DB::$error_handler = false; // since we're catching errors, don't need error handler
DB::$throw_exception_on_error = true;
try {
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
} catch(MeekroDBException $e) {
echo "Error: " . $e->getMessage() . "<br>\n"; // this should be a better error...
echo "SQL Query: " . $e->getQuery() . "<br>\n"; // SELECT email FROM users...
}
...
如果这是 include/include_once 的问题,您会收到完全不同的消息。我首先建议使用最新版本的库,即 2.3,最重要的是,检查你使用的 PHP 的版本。
据我所知,MySQLi 连接很可能存在问题,并且根据您的 PHP 版本,确保连接未失败的检查存在问题,如本页所示http://php.net/manual/en/mysqli.connect-error.php
Warning
The mysqli->connect_error property only works properly as of PHP versions 5.2.9 and 5.3.0. Use the mysqli_connect_error() function if compatibility with earlier PHP versions is required.
由于没有连接,这将导致您在此表单中看到多个警告。
Warning: mysqli::method_name(): invalid object or resource mysqli
我正在为一个学校项目开发一个非常简单的 CMS 系统,但遇到了一些麻烦。
现在我的网页是这样设置的:
- 我创建的用户可以访问的每个网页 - 在顶部和底部,我有一个 include_once(header.php) 和 include_once(footer.php)分别。这些包含应该加载的任何页面的顶部和底部。
在我的 header.php 我有这个,还有其他杂项 HTML:
<?php
require_once('php/constants.php');
require_once("php/functions.php");
?>
对于 constants.php - 包含我能想到的每个已定义常量,因为我宁愿在此处更改一个常量,也不愿在我的代码中搜索以查找我在其他地方可能拥有的任何实例。
For Functions.php - 包含我为在该 CMS 上使用而创建的所有函数。包含从我使用 PHPMailer 的电子邮件功能到使用 MeekroDB 进行数据库交互的注册和登录的所有内容。
问题就出在这里。
在我的帐户注册页面上,有类似这样的内容
//get the username, password, first/last name, and send it to register function.
$firstName = $_POST['first_name'];
$lastName = $_POST['last_name'];
$password = $_POST['password'];
$email_address = $_POST['email_address'];
//register the user. where register() is in functions.php
if(register($email_address, $password, $firstName, $lastName))
{
echo "REGISTRATION SUCCESSFUL";
}
else
{
echo "REGISTRATION UNSUCCESSFUL";
}
在我的 functions.php 文件中,我使用 MeekroDB 提到我使用的 DB::QUERY 赋值来声明我的数据库信息。
//MeekroDB - Used for database CRUDding
require_once("php/meekrodb.php");
DB::$user = 'root'; //yes i'll change this later
DB::$password = ''; //this too
DB::$dbName ="testDB";
在实际的寄存器函数中我有这个:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
//if there is a row in the DB with this email...
if(DB::count() >= 1)
{
return false;
}
else
{
//lets prepare the user for insertion into the database.
$username = $firstName . '_' . $lastName;
//generate a salt for database insertion and password hashing
$salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));
$password = hash('sha512', $password . $salt);
//create the insert statement array for insertion.
$insertionArray =
array(
'username' => $username,
'password' => $password,
'email' => $email,
'salt' => $salt,
'isAdmin' => 0
);
//insert the new user into the database.
DB::INSERT('users', $insertionArray);
//Send a registration email to the new user.
//Send an email to the user stating they've registered.
$mailer = createMailer();
//set the contents of the email
$mailer->MsgHTML(generateEmail_Registration($username));
//set the destination address
$mailer->AddAddress($email, $firstName . ' ' . $lastName);
if($mailer->Send())
{
return true;
}
else
{
return false;
}
}
}
对我来说,这应该可行。
但是:
每当我尝试 运行 register() 函数时,我都会遇到大量涉及 MeekroDB 的错误,而且我不是 100% 确定如何修复它们(无论是 INCLUDE 问题,还是可能是meekroDB 的东西?)
这是我遇到的错误。
Warning: mysqli::set_charset(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 186
Warning: MeekroDB::get(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 188
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::real_escape_string(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 496
Warning: mysqli::query(): invalid object or resource mysqli in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 628
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 662
Warning: MeekroDB::queryHelper(): Property access is not allowed yet in D:\xampp\htdocs\finalproject\php\meekrodb.php on line 663
REGISTRATION UNSUCCESSFUL //of course.
我认为这与我包含各种 PHP 文件的方式有关,并且在 return 中以某种方式扰乱了我的 MeekroDB 连接。
如果你们需要更多信息,请告诉我,但我认为我已经在 table.
上发布了所有内容我不常post来这里,所以如果我在某处错过了某种礼仪,我提前道歉。
谢谢! :)
如果您按照 meekrodb.php 文件进行操作,第一个错误是在您刚刚尝试连接的 $mysql 对象上调用的(查看 meekrodb.php 文件看看我在说什么)所以这些错误可能与连接信息错误或连接到数据库的其他问题有关,因此其他功能失败。
查看更多信息性错误的一种简单方法是尝试使用他们的 $throw_exception_on_error
函数:
function register($email, $password, $firstName, $lastName)
{
//check to see if the user already exists
//poll the DB to see if the email is already in there.
DB::$error_handler = false; // since we're catching errors, don't need error handler
DB::$throw_exception_on_error = true;
try {
$results = DB::QUERY("SELECT email FROM users WHERE email = %s", $email);
} catch(MeekroDBException $e) {
echo "Error: " . $e->getMessage() . "<br>\n"; // this should be a better error...
echo "SQL Query: " . $e->getQuery() . "<br>\n"; // SELECT email FROM users...
}
...
如果这是 include/include_once 的问题,您会收到完全不同的消息。我首先建议使用最新版本的库,即 2.3,最重要的是,检查你使用的 PHP 的版本。
据我所知,MySQLi 连接很可能存在问题,并且根据您的 PHP 版本,确保连接未失败的检查存在问题,如本页所示http://php.net/manual/en/mysqli.connect-error.php
Warning The mysqli->connect_error property only works properly as of PHP versions 5.2.9 and 5.3.0. Use the mysqli_connect_error() function if compatibility with earlier PHP versions is required.
由于没有连接,这将导致您在此表单中看到多个警告。
Warning: mysqli::method_name(): invalid object or resource mysqli