0

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

Mysqli settings

Here are my mysqli settings are these the way they are meant to be?

  • You should firstly select email, and then take the rowCount function from PDO, there is some odd bugg with executing these queries in PDO... – Jordy Jan 05 '15 at 21:02
  • Why are you using `LIKE` here? Why not `WHERE email = ?`? You should also log out your errors. `$conn->error` when the prepare fails. – Mike Brant Jan 05 '15 at 21:04
  • 1
    @Jordy Not sure what you mean, I'm not using PDO but mysqli –  Jan 05 '15 at 21:04
  • @MikeBrant Just tried `WHERE email = ?` same result :( –  Jan 05 '15 at 21:05
  • @Lupy you could try the same thing as for mysqli. $conn->num_rows; – Jordy Jan 05 '15 at 21:06
  • @Jordy He can't use num_rows if he can't get the query to execute. And he currently can't get the query to execute, because he is not able to prepare it. – Mike Brant Jan 05 '15 at 21:07
  • What does `var_dump($conn->error)` show if executed right after your failed prepare? You should always build code to handle failure and log errors and such. Currently your code just assumes everything works fine. – Mike Brant Jan 05 '15 at 21:07
  • @MikeBrant It shows NULL the same as `var_dump($stmt)` –  Jan 05 '15 at 21:09
  • Replace `$stmt->execute();` with `if(!$stmt->execute()){trigger_error("there was an error....".$conn->error, E_USER_WARNING);}` - Add error reporting to the top of your file(s) right after your opening ` – Funk Forty Niner Jan 05 '15 at 21:11
  • Weird that you are getting `NULL` as you should be either getting the `mysqli_stmt` object or `false` if there was a failure. Are you showing all lines of code? – Mike Brant Jan 05 '15 at 21:13
  • @MikeBrant are you asking if I pasted all lines of code in my question? If so no, above the code that I pasted I got all data from my html form, required a file set some variables and at the very end I have a redirect using `header();` –  Jan 05 '15 at 21:20
  • @Lupy I was wondering if there were intermittent lines that were omitted. Seeing your update though it seems clear the problem might be that mysqli extension is not enabled on your server. If you would have checked for `$conn->connect_error` in your code, you would have caught this. As it is, I missed this in that I didn't look at your var_dump for the mysqli object closely enough. You can see the error noted there in your dump. – Mike Brant Jan 05 '15 at 21:25
  • Plus, do up a seperate file with ` – Funk Forty Niner Jan 05 '15 at 21:27
  • @Fred-ii- Done, what am I looking for? API Extensions? –  Jan 05 '15 at 21:32
  • Lupy: That and anything related to `mysql` - `mysqli` - `pdo`. See these also http://stackoverflow.com/q/6285141/ and http://stackoverflow.com/q/2198154/ - @MikeBrant any other suggestions for the OP? – Funk Forty Niner Jan 05 '15 at 21:38
  • @Fred-ii- I'm not sure what I need, do you mind having a look at my updated question? I posted the settings there... –  Jan 05 '15 at 21:45
  • I can't come up with anything. See if you can contact your hosting company. They'd be the ones to speak with in order to get tech support about this; which at this point looks like is what you need. In the meantime, try another type of query without prepared statements to see if you get results. – Funk Forty Niner Jan 05 '15 at 21:56
  • @RyanVincent I this particular scenario I did not even have an email address in my database, all the query does is check for how many rows there are with that particular email address. 0 is a valid result. The problem ended up being with the connection, I used "localhost" as a servername on a shared hosting server, see my answer below. –  Jan 07 '15 at 10:58

1 Answers1

0

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.