2016-01-06 40 views
1

我在Windows 7中使用php和mysql的wamp服務器。有一個連接到mysql數據庫的類,db__connect文件用於建立連接。包含在一個類中的PHP訪問對象。

下面是db_connect的代碼;

<?php 
 

 

 
include_once 'psl-config.php'; // Needed because functions.php is not included 
 
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE); 
 
if ($mysqli->connect_error) { 
 
    header("Location: ../error.php?err=Unable to connect to MySQL"); 
 
    exit(); 
 
}

下面是CLAS代碼:

<?php 
 

 
include_once $_SERVER['DOCUMENT_ROOT'].'/datacentre/admin/includes/db_connect.php'; 
 

 
class User { 
 
\t 
 
\t function getPermission($email, $perms) { 
 
\t 
 
\t \t if ($stmt = $mysqli->prepare("SELECT members.email , groups.permission FROM members 
 
\t \t         JOIN groups ON members.group_id = groups.group_id 
 
\t \t \t \t \t \t \t \t \t \t WHERE email = ? LIMIT 1")) { 
 
     $stmt->bind_param('s', $email); // Bind "$email" to parameter. 
 
     $stmt->execute(); // Execute the prepared query. 
 
     $stmt->store_result(); 
 
     // get variables from result. 
 
     $stmt->bind_result($user_email, $user_perms); 
 
\t \t print "yes we can ".$user_perms; 
 
\t \t exit(); 
 
\t \t 
 
\t \t if($perms & $user_perms) 
 
\t \t {return true; 
 
\t  exit();} 
 
\t 
 
\t return false; 
 
    exit(); 
 
} 
 
\t \t 
 
\t } 
 

 

 
?>

下面是錯誤消息 enter image description here

+0

您在全局範圍內聲明'$ mysqli'(順便說一句壞習慣),然後嘗試在對象方法中使用它。這些是不同的範圍。 – arkascha

回答

0

您需要在用戶類中聲明$ mysqli,但我打賭您會希望在其他類中重用該連接。你會過得更好$傳遞到mysqli的用戶類

new User($email, $perms, $mysqli); 

這當然假定您的全局,而不是實例化另一個類中的User類。

相關問題