0

我收到“致命错误:在非对象上调用成员函数 prepare()”错误,脚本在我的其他主机上运行良好,但现在我移动主机显示此错误并且不知道为什么,因为编码很好。

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);

}

我必须把我的连接信息拿出来让每个人都知道......

4

1 回答 1

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;
}
于 2012-06-09T15:44:38.740 回答