这是我的数据库课程:
<?php
class Database{
private $pdo_conn;
private $dsn, $user, $pass;
// connect to db
public function __construct($username = "root", $password = "", $host = "localhost", $dbname = "test"){
$this->dsn = 'mysql:host='. $host . ';dbname=' . $dbname . ';charset=utf8';
$this->user = $username;
$this->pass = $password;
$this->Start();
}
public static function Start(){
try{
$this->pdo_conn = new PDO($this->dsn, $this->user, $this->pass);
} catch(PDOException $e) {
print_r($e);
exit();
}
}
//Insert, Delete and Create
public static function RunQuery($sql, $inputs = null){
$pdo = $this->pdo_conn; //PDO connection.
if(is_null($inputs)){ //If there are no any inputs (input array)
$stmt = $pdo->query($sql); //returning a result set as a PDO object.
} else {
try{
$stmt = $pdo->prepare($sql); //Prepares the statment
if($stmt){ //If there is no error
$stmt->execute($inputs); //Pass the input array to the statment exacute.
} else {
print_r("Doesn't work!");
}
} catch(PDOException $e){
print_r($e); //Print errors array.
exit();
}
}
return $stmt;
}
这是我的用户类别:
<?php
class Users{
public function GetUser($id){
$sql = "SELECT * FROM guests WHERE id = :id";
$inputs = array(':id' => $id);
$stmt = Database::RunQuery($sql, $inputs);
$rowCount = $stmt->rowCount();
if($rowCount > 0)
return $stmt->fetchAll(PDO::FETCH_ASSOC);
else //If there is any data in Database (row available)
return $rowCount;
}
}
?>
这是错误:
Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\ITDHOST\core\classes\database.php:31 Stack trace: #0 C:\xampp\htdocs\ITDHOST\core\classes\users.php(10): Database::RunQuery('SELECT * FROM g...', Array) #1 C:\xampp\htdocs\ITDHOST\core\pieces\connection\login.php(8): Users::GetUser(1) #2 C:\xampp\htdocs\ITDHOST\core\functions.php(25): require_once('C:\\xampp\\htdocs...') #3 C:\xampp\htdocs\ITDHOST\core\pages\connection.php(4): include_piece('connection', 'login') #4 C:\xampp\htdocs\ITDHOST\core\functions.php(12): require_once('C:\\xampp\\htdocs...') #5 C:\xampp\htdocs\ITDHOST\core\init.php(18): include_page('connection') #6 C:\xampp\htdocs\ITDHOST\index.php(3): include('C:\\xampp\\htdocs...') #7 {main} thrown in C:\xampp\htdocs\ITDHOST\core\classes\database.php on line 31
[请帮帮我,非常感谢!
我遇到了一些不同的错误,我什至都不知道如何解决它们并使之正常工作,我使用SPL加载器加载类,并且需要帮助来解决此错误!这是我的数据库类:
class CalculationsTask4
{
public void someMethod(int endLoop, int startLoop)
{
int answer = 0;
for (int i = startLoop; i < endLoop; i++)
{
answer = answer +i;
}
MessageBox.Show("answer =" + answer.ToString());
}