简体   繁体   English

在PHP中使用bind_param获取错误

[英]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 . 我收到错误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 如果这有什么用或有帮助,我正在使用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();

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM