0

I'm trying to use this script to see if there's a registered using in my database, but I'm having trouble on line 6. I keep getting the error:

Fatal error: Call to a member function bind_param() on a non-object in -redacted-/check_login.php on line 6

I'm stumped as to why it's failing, because I have a similar statement working on the create user function.

<?php
ini_set('display_errors', 'On');
$db = new mysqli("localhost", "root", "-redacted-", "-redacted-");

$query = $db->prepare("SELECT user FROM users WHERE username = ? AND password = ?");
$query->bind_param('ss', $_POST['username'], md5($_POST['password']));
$query->execute();
$query->bind_result($result);
$query->fetch();

if($result->num_rows == 1) {
    session_start();
    $_SESSION['user'] = $_POST['username'];
    header("Location: 10.0.0.15/index.php");
}
?>
2
  • That means prepare() got an error and returned false instead of a mysqli_stmt object. You need to check for errors. Commented Jun 30, 2013 at 2:57
  • I expect that your $query is failing - check the return value from the database and see what's happening when it runs. Commented Jun 30, 2013 at 2:58

1 Answer 1

1

This is usually caused by referencing a non-existant column/table in your database schema. Hence why the prepared is the root cause of this error.

What you should do, is check over your query. Making sure you are:

  1. not using reserved mysql keywords
  2. referencing columns/tables that exist within the schema
  3. There are no syntax errors within your SQL Query
Sign up to request clarification or add additional context in comments.

5 Comments

I presume it was not down to a non-existant reference, it was down to using the reserved word user?
Correct. It just decided it was going to throw a fit over it. I switched name and it started working perfectly.
It's within best practice to avoid using reserved words, but if it's a must. You can escape the words using a back tick.. Example: SELECT * FROM Table WHERE ``user``=? This will tell MySQL that you a referncing a column name and not trying to use a reserved keyword -- Note, only use one backtick each side of the reserved word. There is a formatting problem with SO That I don't know how to overcome.
Okay, it's not a big deal for me to have it named the way it was. I'll just keep out of the keywords. On a slightly different note, Is there an easy way to count how many results are coming back? The above gets rejected as a non-object.
@Jacobm001 I presume your looking for: php.net/manual/en/mysqli-stmt.num-rows.php which is what springs to mind, to count the current results. If thats not what your asking, then unfortunatly I do not know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.