php - PHP/Slim 有效的 MySQL 查询准备语句单独工作,但一起使用时失败
问题描述
我正在使用带有 Slim 框架的 Php 开发一个 RESTApi,以将 Android 应用程序连接到 MySQL 数据库(现在使用 Postman 发送测试请求)。该应用程序是我所在城镇现场音乐活动的歌曲请求应用程序 - 参加演出的每个艺术家都将他们的歌曲添加到应用程序中,演出的参与者可以选择并请求一首歌曲,该歌曲会向艺术家发送电子邮件,并附上他们的在队列中的位置。
我正在尝试插入一个名为 setqueues(ShowID int, SongID int, ArtistID int, Position int) 的表,但首先我必须查看给定的 ShowID 是否有任何歌曲分配给它。如果是,我将返回的行数加 1,并将其分配给一个名为 $pos; 的变量;否则 $pos = 1。然后我尝试在表中插入一个新行,但我得到
在布尔值上调用成员函数 bind_param()
每当我尝试这样做时。我的代码是:
索引.php
$app->post('/songtoqueue', function(Request $request, Response $response){
if(!haveEmptyParameters(array('show_id', 'song_id', 'artist_id'), $request, $response)){
$request_data = $request->getParsedBody();
$show_id = $request_data['show_id'];
$song_id = $request_data['song_id'];
$artist_id = $request_data['artist_id'];
$db = new DbOperations;
$result = $db->addSongToShow($show_id, $song_id, $artist_id);
if($result == SHOW_SONG_ADDED){
$message = array();
$message['error'] = false;
$message['message'] = 'Song added to Show successfully';
$response->write(json_encode($message));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(201);
}else if($result == SHOW_SONG_FAILURE){
$message = array();
$message['error'] = true;
$message['message'] = 'Some error occurred while attempting to add Song to Show.';
$response->write(json_encode($message));
return $response
->withHeader('Content-type', 'application/json')
->withStatus(422);
}
}
return $response
->withHeader('Content-type', 'application/json')
->withStatus(422);
});
DbOperations.php
public function addSongToShow($show_id, $song_id, $artist_id){
$stmt = $this->con->prepare("SELECT COUNT(*) FROM (SELECT * FROM setqueues WHERE ShowID = ?) as c");
$stmt->bind_param("i", $show_id);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$pos = 0;
if($count == 0){
$pos = 1;
}else{
$pos = $count + 1;
}
$stmt = $this->con->prepare("INSERT INTO setqueues(ShowID, SongID, ArtistID, Position) VALUES (?,?,?,?)");
$stmt->bind_param("iiii", $show_id, $song_id, $artist_id, $pos);
if($stmt->execute()){
return SHOW_SONG_ADDED;
}
return SHOW_SONG_FAILURE;
}
数据库连接.php
<?php
class DbConnect{
private $con;
function connect(){
include_once dirname(__FILE__) . '/Constants.php';
$this->con = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(mysqli_connect_errno()){
echo "Failed to connect" . mysqli_connect_error();
return null;
}
return $this->con;
}
}
我已经检查了查询在 phpmyadmin 中是否正常运行,并且如果我只使用一个或另一个,它们可以正常工作,但是任何时候我尝试执行这两个代码都会失败并且我得到上面的错误,有问题的行是
$stmt = $this->con->prepare("INSERT INTO setqueues(ShowID, SongID, ArtistID, Position) VALUES (?,?,?,?)");
这里已经有很多问题的解决方案是不正确的 MySQL 语句;但是,我确信这些查询是正确的,甚至尝试将 phpmyadmin 中自动生成的插入语句复制/粘贴到我的代码中。问题是同时使用上述两个查询。任何意见是极大的赞赏
--UPDATE-- 通过启用 mysqli 错误报告来解决这个问题
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
在我的连接声明之前。这让我看到我需要添加 $stmt->close(); 在我的查询之间,因为 mysqli 默认使用无缓冲查询
解决方案
推荐阅读
- spring - Spring如何重用关系上的规范
- java - 获取不受支持的字段:在 Java Spring Boot 中使用 DateTimeFormatter 时的 DayOfMonth
- python - numpy数组中所有元素的范围
- python - 在 tkinter 中删除特定的小部件
- javascript - Next.js:优化 LCP 时间/速度
- html - 如何更改 2x2 网格中 div 的顺序
- javascript - 如何模拟返回图像缓冲区的 axios 调用
- angular - 如何存储不同域的cookie
- flutter - 如何在 Flutter 中通过 package:in_app_purchase 将单个 IAP 产品用于多个项目
- azure - 带有条件的自动缩放实例计数的 Azure App-Service 警报