使用pdo怎么實現登錄
錯誤Fatal error: ?Uncaught Error: Call to a member function fetchAll() on null in F:\xampp\htdocs\shopDxg\shopDxg\lib\mysql.func.php:116 Stack trace: #0 F:\xampp\htdocs\shopDxg\shopDxg\lib\mysql.func.php(96): PdoMySQL::fetchAll('SELECT * FROM u...') #1 F:\xampp\htdocs\shopDxg\shopDxg\core\admin.inc.php(7): PdoMySQL::find('user', NULL, '*', NULL, NULL, NULL, NULL) #2 F:\xampp\htdocs\shopDxg\shopDxg\admin\doLogin.php(15): checkAdmin('user', NULL) #3 {main} ?thrown in F:\xampp\htdocs\shopDxg\shopDxg\lib\mysql.func.php on line 116
代碼
// 得到結果集中所有記錄 ...
public static function fetchAll($sql=null)
{
? ? if($sql!=null){
? ? ? ? ? ? self::query($sql);
? ? }
? ? $result=self::$PDOStatement->fetchAll(constant("PDO::FETCH_ASSOC"));
? ? return $result;
}
2017-04-15
我是采用面向對象的封裝的連接數據庫,封裝數據庫操作等等的。。
function fetchAll($sql,$result_type=MYSQLI_ASSOC){
? ? $conn=connect();
? ? $result=$conn->query($sql);
? ? if ($result->num_rows > 0) {
? ? ? ? // 輸出每行數據
? ? ? ? while($row = $result->fetch_assoc()) {
? ? ? ? ? ? $rows[]=$row;
? ? ? ? }
? ? } else {
? ? ? ? echo "0 個結果";
? ? }
? ? return $rows;
}
我的得到結果集中所有記錄 函數
function connect(){
// ? ?mysql.connect("localhost","root","" or die("數據庫連接失敗Error:".mysql_errno().":".mysql_error()));
// ? ?$link=mysqli.connect("localhost","root","" or die("數據庫連接失敗Error:".mysqli_errno().":".mysqli_error()));
// ? ?$link=new mysqli(DB_HOST,DB_USER,DB_PWD,DB_DBNAME) or die("數據庫連接失敗Error:".mysql_errno().":".mysql_error());
? ? $link= new mysqli(DB_HOST,DB_USER,DB_PWD,DB_DBNAME);
? ? mysqli_set_charset($link,DB_CHARSET);
// 檢測連接
? ? if ($link->connect_error) {
? ? ? ? die("連接失敗: " . $link->connect_error);
? ? }
? ? return $link;
}
這是連接數據庫封裝好的,面向對象的
希望能幫到你!一起學習