Francisco F. Francisco F. -4 years ago 106
PHP Question

Getting error with bind_param in php

I'm getting the error

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean
. I have tried all I know, and I cannot get it to work. This is what I have so far:

$db = "THIS IS CORRECT, TRUST ME. I HAVE TESTED IT :)";
$team = mysqli_real_escape_string($db, $_POST['team']);
$sqlcheckteam = $db->prepare("SELECT teamnum FROM teams WHERE teamnum=?");
$sqlcheckteam->bind_param('s', $bindteam);
$bindteam = $team;
$sqlcheckteam->execute();


The weird thing is that it works on my localhost server, but not on my actual server.

Any help would be appreciated.

If this has anything to do or helps, I'm using PHP 7.1.2




If I could get help with uploading this, it would be great :)

$nickname = mysqli_real_escape_string($mysqli, $_POST['nickname']);
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$passcreate = password_hash($_POST['password'], PASSWORD_DEFAULT);
$sqlinsertteam = $mysqli->prepare("INSERT INTO teams(teamnum, teamname, email, password) VALUES(?, ?, ?, ?)");
$sqlinsertteam->bind_param("ssss", $bindteam, $bindnickname, $bindemail, $bindpassword);
$bindteam = $team;
$bindnickname = $nickname;
$bindemail = $email;
$bindpassword = $passcreate;
$sqlinsertteam->execute();

Answer Source

You should incorporate some error checking to see what the actual issue is. Try this for example...

# try statement to catch thrown exceptions (error messages)
try {

    # Make MYSQLI Connection
    $mysqli = new mysqli("host", "user", "password", "database");

    if ( $mysqli->connect_errno ) {

        # Throw connections error message
        throw new Exception("Error, could not connect to database.");

    }

    # Prepare your query for execution
    $stmt = $mysqli->prepare("SELECT `teamnum` FROM `teams` WHERE `teamnum`= ?");

    # Bind the two parameters to your statement
    $stmt->bind_param("i", $_POST['team']);

    if ( $stmt === false ) {

        # Throw Exception (error message)
        throw new Exception("Error, could not process data submitted.");

    }

    # Excecute your query
    $stmt->execute();

    if ( $stmt === false ) {

        # Throw Exception (error message)
        throw new Exception("Error, count not execute database query.");

    }

    # Bind the results to a variable
    $stmt->bind_result($teamnum);

    # Fetch your data as an associative array [ "key" => "value" ]
    while($stmt->fetch()){

        $mynumber = $teamnum;

    }

    if ( $stmt === false ) {

        # Throw Exception (error message)
        throw new Exception("Error, could not get results from database.");

    }

    #Prepare an INSERT query to insert into database
    $stmt = $mysqli->prepare("INSERT INTO `teamnum` ( `teamnum` ) VALUES ( ? )");

    # Bind the two parameters to your statement
    $stmt->bind_param("i", $mynumber);

    if ( $stmt === false ) {

        # Throw Exception (error message)
        throw new Exception("Error, could not process to insert.");

    }

    # Excecute your query
    $stmt->execute();

    if ( $stmt === false ) {

        # Throw Exception (error message)
        throw new Exception("Error, count not execute database query.");

    }

    # close your statement
    $stmt->close();

}

# Catch any exceptions thrown and output the error
catch( Exception $e ) {

    # Check if statement is still open and close it
    if($stmt){
        $stmt->close();
    }

    # Create your error response
    die($e->getMessage());

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download