0

$stmt variable is null means it doesn't contain my query even though connection($con) to the database is created.

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\WebServersAndroid\includes\DbOperations.php:24 Stack trace: #0 C:\xampp\htdocs\WebServersAndroid\v1\registerUser.php(12): DbOperation->createUser('syed', '1997') #1 {main} thrown in C:\xampp\htdocs\WebServersAndroid\includes\DbOperations.php on line 24

error occurs in bind_param maybe because there is no query to store data to. Please tell me what is the problem and why $stmt is null. Thank you

    class DbOperation{

        private $con;

        function __construct(){

            require_once dirname(__FILE__).'/DbConnect.php';

            $db = new DbConnect();

            $this->con = $db->connect();

        }


        function createUser($name,$usn){

        $query = "INSERT INTO 'test_col'('name','usn') VALUES (?,?); ";
        $stmt = $this->con->prepare($query);
        echo $stmt;
        if($stmt != null)
        {
            $stmt->bind_param("ss",$name,$usn);
        if($stmt -> execute())
        {
            return true;
        }else{
            return false;
        }

        }
        else{
            echo "in else";
        }

    }
    }

1 Answers1

-1

I think this code generates a lot of errors.

$query = "INSERT INTO 'test_col'('name','usn') VALUES (?,?); ";

values (?, ?) expects 2 values to be bind later.

While this code

`$stmt->bind_param("ss",$name,$usn);`

binds 3 parameters which trigger an error in binding.

Next, the use of single/double quotation mark is interpreted by MySQL as String literal as stated in documantation

As a result, 'test_col'('name','usn') causes error, you should use ` or just leave the name of table and columns instead of using single quotation mark (') which translate your table name, database name, and/or column name to string instead of reference.

Itami
  • 78
  • 1
  • 8
  • I suggest you read the documentation for [`mysqli_stmt::bind_param()`](http://php.net/manual/mysqli-stmt.bind-param.php). OP is not binding three parameters – Phil Oct 17 '18 at 02:38
  • My bad, I should have read that. – Itami Oct 17 '18 at 08:06