在php中使用bind_param获取错误

时间:2017-03-24 03:44:32

标签: php sql prepared-statement bindparam

我收到错误Fatal error: Uncaught Error: Call to a member function bind_param() on boolean。我已经尝试了所有我知道的,但我无法让它发挥作用。这就是我到目前为止所做的:

$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();

奇怪的是它可以在我的localhost服务器上运行,但不能在我的实际服务器上运行。

任何帮助都将不胜感激。

如果这有任何关系或帮助,我使用的是PHP 7.1.2

如果我可以获得上传帮助,那就太棒了:)。

$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();

1 个答案:

答案 0 :(得分:1)

您应该加入一些错误检查以查看实际问题。试试这个例子......

# 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());

}