2012-06-09 96 views
0

Im得到「致命錯誤:調用成員函數prepare()在一個非對象」Error腳本在我的其他主機上正常工作但現在移動主機顯示這個錯誤,不知道爲什麼,因爲編碼是好的。致命錯誤:調用成員函數prepare()在pdo中的非對象

include 'functions/functions.php'; 
global $db; 

$db = mysqlconnect(); 

$password = md5($_POST['mypassword']); 

$mod = '1' ; 
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ? And mod = ?"); 
$statement->execute(array($_POST['myusername'],$password, $mod)); 

$count = $statement->rowCount(); 


if($count == 1){ 
    $db = mysqlconnect(); 
    // Register $myusername, $mypassword and redirect to file "login_success.php" 

$_SESSION['user'] = $_POST['myusername'] ; 

//Test if it is a shared client 
if (!empty($_SERVER['HTTP_CLIENT_IP'])){ 
    $ip=$_SERVER['HTTP_CLIENT_IP']; 
//Is it a proxy address 
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){ 
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; 
}else{ 
    $ip=$_SERVER['REMOTE_ADDR']; 
} 



$sqll = "UPDATE users SET lastip=? WHERE username=?"; 
    $q = $db->prepare($sqll); 
    $q->execute(array($ip,$_SESSION['username'])); 

$_SESSION['user'] = $_POST['myusername'] ; 

$sqlll = "INSERT INTO user_log (username,ip) VALUES (?, ?)"; 
    $qq = $db->prepare($sqlll); 
    $qq->execute(array($_SESSION['username'],$ip)); 




header("Location: home.php"); 
} else { 
    echo "Wrong Username or Password"; 
} 

中有你可以看到它說的準備是錯誤的在這條線

$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ? And mod = ?"); 

時,有什麼不對,我可以看到代碼....

這裏是我的功能文件,其中包括mysqlconnect

function mysqlconnect(){ 
    global $db; 
    $host = 'localhost'; 
    $port = 3306; // This is the default port for MySQL 
    $database = ''; 
    $username = ''; 
    $password = ''; 




    // Construct the DSN, or "Data Source Name". Really, it's just a fancy name 
    // for a string that says what type of server we're connecting to, and how 
    // to connect to it. As long as the above is filled out, this line is all 
    // you need :) 
    $dsn = "mysql:host=$host;port=$port;dbname=$database"; 

    // Connect! 
    $db = new PDO($dsn, $username, $password); 

} 

我已經讓我的連接信息出來,所以每一個ne知道...

+2

你的函數'mysqlconnect()'看起來像什麼?它是否返回一個PDO對象? – Jonathan

+0

我編輯了第一篇文章,連接 – user1405062

回答

1

當指出$db = mysqlconnect();時,您希望mysqlconnect()返回一個PDO對象。更改功能,使其工作:

function mysqlconnect(){ 
    $host = 'localhost'; 
    $port = 3306; // This is the default port for MySQL 
    $database = ''; 
    $username = ''; 
    $password = ''; 

    // Construct the DSN, or "Data Source Name". Really, it's just a fancy name 
    // for a string that says what type of server we're connecting to, and how 
    // to connect to it. As long as the above is filled out, this line is all 
    // you need :) 
    $dsn = "mysql:host=$host;port=$port;dbname=$database"; 

    // Connect! 
    $db = new PDO($dsn, $username, $password); 

    // Return PDO object 
    return $db; 
} 
+0

現在它只是飛到其他人和說錯誤的用戶名或密碼時,密碼是正確的(即使在分貝),但沒有錯誤 – user1405062

相關問題