php - 如何从准备语句 PHP 中收集结果?
问题描述
我无法在此函数中收集查询结果:
<?php
function getBestScore()
{
require_once ('connection.php');
$idJoueur = session_id();
if ($stmt = $conn->prepare('SELECT bestScore FROM Player WHERE ID = ?'))
{
$stmt->bind_param('i', $idJoueur);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
$bestScore = $data;
if (!(empty($bestScore)))
{
header("Location: ../index.php");
}
return $bestScore;
}
else
{
$error = $conn->errno . ' ' . $conn->error;
echo $error;
}
}
我的连接有效,我尝试过fetch_assoc()
,store_result()
但每次都失败。
解决方案
如果要选择数据,请尝试以下操作
if ($stmt = $conn->prepare('SELECT bestScore FROM Player WHERE ID = ?')) {
$stmt->bind_param('i',$idJoueur);
$stmt->execute();
/* bind result variables */
$stmt->bind_result($result);
/* fetch value */
$stmt->fetch();
if (!(empty($result))) {
header("Location: ../index.php");
}
return $result;
}
希望这可以帮助。
推荐阅读
- android - 将时间选择器限制为两个小时之间的时间选择
- angular - Angular 2 ViewChild NgModel 设置新值
- php - 如何使用 PHP 将变量从一个页面传递到另一个页面?
- sql - 从 JSON 列中的数组获取数据作为行
- php - Dropzone JS表单数据不发布到数据库
- python - pandas idxmax:在平局的情况下返回所有行
- php - 如何在laravel中编码一个字符
- swisscomdev - Swisscom 应用程序云上的应用程序性能监控
- android - UnityWebRequest.SendWebRequest 不在移动设备上发送
- javascript - Angularjs将数据表下载到csv