0

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();
Francisco F.
  • 111
  • 1
  • 3
  • 14
  • change your code order like this $bindteam = $team; $sqlcheckteam->bind_param('s', $bindteam); – JYoThI Mar 24 '17 at 03:56
  • It looks like you are mixing procedural and object oriented mysqli. Check out the manual for real_escape_string and try switching it to OO style as well. https://secure.php.net/manual/en/mysqli.real-escape-string.php – tjfo Mar 24 '17 at 04:04
  • @tjfo Would you mind explaining what you mean by procedural and object oriented, and OO style? Sorry, I don't understand what it means – Francisco F. Mar 24 '17 at 04:37
  • if your trying to use prepared statements you're doing it wrong as you don't need to escape with prepared statements as that is handled for you. – Shane Henry Mar 24 '17 at 04:41
  • @ShaneHenry I am trying to use prepared statements, but on my localhost server I don't have problems with get_result(), but in my actual server I get an error, and I read that I should use bind_param(). I am very confused right now – Francisco F. Mar 24 '17 at 04:42

1 Answers1

1

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
    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 `TABLE_NAME` ( `COLUMN_NAME` ) 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();

    # Kill Connection
    $thread = $mysqli->thread_id;
    $mysqli->kill($thread);

}

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

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

    if($mysqli){
        # Kill Connection
        $thread = $mysqli->thread_id;
        $mysqli->kill($thread);
    }

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

}
Shane Henry
  • 308
  • 1
  • 7