I know there is a ton of other questions like this, but none that I found answered my problem.
I get the following error Call to a member function bind_param() on a non-object
with the following code:
$servername = "localhost";
$username = "MyUsername";
$password = "MyPassword";
$dbname = "MyDatabase";
$conn = new mysqli($servername, $username, $password, $dbname);
var_dump($conn);
$stmt = $conn->prepare("SELECT COUNT(email) FROM subscribers WHERE email LIKE ?");
var_dump($stmt);
$stmt->bind_param("s", $email);
$stmt->execute();
var_dump($conn) returns:
object(mysqli)#2 (19) { ["affected_rows"]=> NULL ["client_info"]=> NULL ["client_version"]=> int(50011) ["connect_errno"]=> int(2002) ["connect_error"]=> string(25) "No such file or directory" ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["field_count"]=> NULL ["host_info"]=> NULL ["info"]=> NULL ["insert_id"]=> NULL ["server_info"]=> NULL ["server_version"]=> NULL ["stat"]=> NULL ["sqlstate"]=> NULL ["protocol_version"]=> NULL ["thread_id"]=> NULL ["warning_count"]=> NULL }
and var_dump($stmt) returns:
NULL
My query is correct, I tried it out in phpMyAdmin, it works.
The odd thing is that this exact code worked on my localhost, after uploading it it stopped working.
EDIT
After replacing $stmt->execute();
with
if(!$stmt->execute()){trigger_error("there was an error....".$conn->error, E_USER_WARNING);}
And adding error_reporting(E_ALL); ini_set('display_errors', 1);
I get the following errors:
Warning: mysqli::mysqli(): (HY000/2002): No such file or directory in [...]
Warning: mysqli::prepare(): Couldn't fetch mysqli in [...]
Warning: main(): Couldn't fetch mysqli in [...]
EDIT 2
Here are my mysqli settings are these the way they are meant to be?
So I contacted the hosting company and the solution was quite simple really. While connecting to the database I used "localhost" as a servername which does not work when you are on a shared hosting service. All I needed to do is to change it to the servername specified in my MySQL configuration.