我试图遍历我的bind_results,但它不像我需要的那样工作。我尝试过这20种不同的方式,结果总是一样的。我只获得$survey_user_id
和$survey_result
的第一个记录来回显。
当我按照我在查询中执行的COUNT
时,我是否还需要循环执行此操作?因为截至目前,当rating_draft_result.id有多个id时,它只计算1条记录。
try {
$survey_title_stmt = $con->prepare("SELECT COUNT(rating_draft_result.id), rating_draft_result.user_id, rating_draft_result.result
FROM rating_draft_result
INNER JOIN users ON
rating_draft_result.user_id = users.id
AND rating_draft_result.result = users.id");
if ( !$survey_title_stmt || $con->error ) {
// Check Errors for prepare
die('Survey SELECT total count prepare() failed: ' . htmlspecialchars($con->error));
}
if(!$survey_title_stmt->execute()) {
die('Survey SELECT total count execute() failed: ' . htmlspecialchars($survey_title_stmt->error));
}
$survey_title_stmt->store_result();
} catch (Exception $e ) {
die("Survey SELECT total count: " . $e->getMessage());
}
$survey_title_stmt->bind_result($survey_id, $survey_user_id, $survey_result);
while ($survey_title_stmt->fetch()) {
$survey_id;
$survey_user_id;
$survey_result;
}
?>
<div class="survey_results_out">
<div id="survey_results_title">Survey Results</div>
<div class="survey_popup_container">
<a class="survey_popup" href="javascript:void(0)">Who drafted best results</a>
<div id="">Users that completed this survey</div><?php echo $survey_id; ?>
<div class="survey_popup_wrap light_admin">
<a class="close_survey_popup" href="javascript:void(0)">X</a>
<div id="indexpopupTitleWrap">
<div id="indexpopupTitle">Results of who drafted the best</div>
</div>
<div id="survey_results_content_wrap">
<div id="survey_results_content_usernames">
<?php
echo $survey_user_id;
echo $survey_result;
?>
我的数据库表看起来像这样......
users
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`lastname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`phone_number` varchar(15) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`salt` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`joined` datetime NOT NULL,
`group` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=105 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
rating_draft_result
CREATE TABLE `rating_draft_result` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`result` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=28 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
期望的输出:
users id``firstname``lastname``email``phone_number``username``password``salt``joined group
1 Jack Johnson jack@email.com 2222 jackusername ffd fdddfd today 1
10 Tom Thompson fdfdfddf@fef.com 5555 Tomusername
rating_draft_result
`id` `user_id` `result`
20 1 10
我希望能够将rating_draft_result的user_id和结果与用户表的id字段匹配。
所以我想获取jackusername和tomusername字段。
答案 0 :(得分:1)
不太确定你想要的数据是什么。
但是以下内容可能有所帮助: -
这将获得 rating_draft_result.id 以及随之而来的2个用户名
SET _JAVA_OPTIONS = -Xms512m -Xmx1024m
这会获得rating_draft_result表中每对用户名的 rating_draft_result.id 的计数
SELECT rating_draft_result.id, u1.username, u2.username
FROM rating_draft_result
INNER JOIN users u1 ON rating_draft_result.user_id = u1.id
INNER JOIN users u2 ON rating_draft_result.result = u2.id