当我去index.php时出错:
致命错误:在第10行的C:\ xampp \ htdocs \ barafranca.nl \ config \ main_functions.php中调用未定义的方法DB :: quote()
奇怪的是,当我在main_functions.php中测试这个(index.php上的php代码)时,一切正常。 但是当我在index.php页面上包含main_functions.php并使用getAccountQuery函数时,它就不能再找到我的Db()对象的方法。
PHP_VERSION:5.5.30。
db.php中:
<?php
class Db {
// The database connection
protected static $connection;
/**
* Connect to the database
*
*
*/
public function connect() {
// Try and connect to the database
if(!isset(self::$connection)) {
self::$connection = mysqli_connect('localhost', 'root', '', 'bfr');
}
// If connection was not successful, handle the error
if(self::$connection === false) {
// Handle error - notify administrator, log to a file, show an error screen, etc.
//return false;
return mysqli_connect_error();
}
return self::$connection;
}
/**
* Query the database
*
* @param $query The query string
* @return mixed The result of the mysqli::query() function
*/
public function query($query) {
// Connect to the database
$connection = $this -> connect();
// Query the database
$result = $connection -> query($query);
return $result;
}
/**
* Fetch rows from the database (SELECT query)
*
* @param $query The query string
* @return bool False on failure / array Database rows on success
*/
public function select($query) {
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
/**
* Fetch the last error from the database
*
* @return string Database error message
*/
public function error() {
$connection = $this -> connect();
return $connection -> error;
}
/**
* Quote and escape value for use in a database query
*
* @param string $value The value to be quoted and escaped
* @return string The quoted and escaped string
*/
public function quote($value) {
$connection = $this -> connect();
return "'" . $connection -> real_escape_string($value) . "'";
}
}
?>
main_functions.php
<?php
include("Db.php");
function getAccountQuery($username)
{
// Database object
$db = new Db();
// Quote and escape parameter value
$username = $db -> quote($username);
$sql = "SELECT * FROM `users` WHERE username=". $username;
$result = $db -> select($sql);
// If result matched $username, table row must be 1 row
if(!empty($result))
{
echo "NOT EMPTY";
return $result[0];
}else {
return null;
}
}
function getOnlineUsersCount($con)
{
$usersOnlineNow = $con->query("SELECT * FROM users WHERE online >= NOW()-500");
return $usersOnlineNow->num_rows;
}
?>
的index.php
<?php
include("config/config.php");
include("config/main_functions.php");
session_start();
$test = getAccountQuery("Nick");
echo $test;
?>
的config.php
<?php
$siteName = "CrimeGame";
$siteDomain = "CrimeGame.nl";
$siteMail = "admin@CrimeGame.nl";
?>