0

I need to check username in my mysql database whether the usernsme is present or not. Below is my php code. When I enter username it shows Fatal error: Call to a member function bind_param() on boolean in ...

This is php code:

<?php
if (isset($_POST['submit'])) {

               $mobile = $_POST['mobile'];

                $sql = 'SELECT `mobile_message` 
                        WHERE `mobile` = ?';


                // Prepare the SQL statement for execution.
                $statement = $connection->prepare($sql);

                $bound = $statement->bind_param('s', $mobile);

                // Execute the prepared statement.
                $statement->execute();
                $statement->store_result();
                if($statement->num_rows > 0) {


                $_SESSION['e']="This username is already exist. Please try different.";
                header('location:sign.php');
                exit;
        } 
    }

?>
dipak dutta
  • 282
  • 2
  • 12
  • 2
    Select from what? You forgot the table name. – jeroen Dec 09 '17 at 15:35
  • 1
    your code should be like this: `$sql = SELECT * FROM table_name WHERE mobile = ?; ` – acoder Dec 09 '17 at 15:37
  • Adding error checking prevents errors like this from happening, as if you had first checked if $statement was valid, you would have seen it was giving a SQL error (cannot execute bind_param on a value of FALSE). – IncredibleHat Dec 09 '17 at 15:46
  • @acoder thank you so much. It is a shame to me, I have done such a mistake!!! – dipak dutta Dec 09 '17 at 15:50
  • @dipakdutta There is no shame man. know more, do more and do better. – acoder Dec 09 '17 at 15:51
  • @Randall the code in the answers for the question you proposed as a duplicated is rather outdated and could be considered even harmful. I've got another answer with much better security and usability for mysqli error reporting. – Your Common Sense Dec 09 '17 at 16:01
  • 1
    @acoder If you give the answer, I would accept you. – dipak dutta Dec 09 '17 at 16:01
  • I can't answer now. Because your question already rewarded as DUPLICATE QUESTION. no matter. carry on. – acoder Dec 09 '17 at 16:03
  • 1
    @Your Common Sense Thank you. I must follow your suggestion from this time regarding error checking. I want to know whether I will put this line `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` before or after `session_start`? Plz advise. – dipak dutta Dec 09 '17 at 16:11
  • @dipakdutta it has nothing to do with sessions. it should be put before mysqli connect code – Your Common Sense Dec 09 '17 at 16:17

0 Answers0