在php中获取bind_参数时出错
我得到一个错误在php中获取bind_参数时出错,php,sql,prepared-statement,bindparam,Php,Sql,Prepared Statement,Bindparam,我得到一个错误致命错误:未捕获错误:调用布尔值上的成员函数bind_param()。我已经尝试了我所知道的一切,但我无法让它发挥作用。这就是我到目前为止所做的: $db = "THIS IS CORRECT, TRUST ME. I HAVE TESTED IT :)"; $team = mysqli_real_escape_string($db, $_POST['team']); $sqlcheckteam = $db->prepare("SELECT teamnum FROM team
致命错误:未捕获错误:调用布尔值上的成员函数bind_param()。我已经尝试了我所知道的一切,但我无法让它发挥作用。这就是我到目前为止所做的:
$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();
奇怪的是,它可以在我的本地主机服务器上工作,但不能在我的实际服务器上工作
任何帮助都将不胜感激
如果这有什么关系或帮助的话,我使用的是PHP7.1.2
如果我能得到上传这个的帮助,那就太好了:)
您应该合并一些错误检查,以查看实际问题是什么。试试这个例子
# 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());
}
更改代码顺序,如$bindteam=$team$sqlcheckteam->bind_参数('s',$bindteam);看起来您混合了过程化和面向对象的mysqli。请查看手册中的真实字符串,并尝试将其切换为OO样式@tjfo你介意解释一下你所说的过程化、面向对象和OO风格是什么意思吗?抱歉,我不明白这意味着什么如果你试图使用准备好的语句,你就做错了,因为你不需要像为你处理的那样用准备好的语句逃逸。@ShaneHenry我试图使用准备好的语句,但在我的localhost服务器上,我的get_result()没有问题,但在我的实际服务器上,我得到了一个错误,我读到应该使用bind_param()。我现在很困惑,这起作用了,谢谢!还有一个问题:我还有一个问题。现在我需要帮助上传到数据库。(我会在底部的问题中加上)很简单。。。。在关闭之前使用相同的连接。准备一个新查询,绑定数据并执行。我已经更新了我的代码,给大家展示了一个例子。非常感谢!谢谢你,先生:-)让我们来。
# 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());
}