2012-06-09 73 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

相关问题