Im getting ” Fatal error: Call to a member function prepare() on a non-object ” Error the script worked fine on my other hosts but now ive moved hosts its showing this error and donno why because the coding is fine.
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";
}
Has you can see its saying the prepare is wrong on this line
$statement = $db->prepare("SELECT * FROM users WHERE username = ? AND password = ? And mod = ?");
when there is nothing wrong with the code which i can see….
Here is my function file which inculdes the 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);
}
I have toke my connect info out just so everyone knows…
When stating
$db = mysqlconnect();, you expectmysqlconnect()to return a PDO object. Change the function to this to make it working: