php - mysqli 获取记录

标签 php mysql mysqli mysqlimport picturegallery

这是关于一个帖子 Show three images from each user对于我遇到的相同问题,那里有很好的解决方案。

if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p2.picture as pic2, p3.picture as pic3
FROM
  pictures p1 left join pictures p2
  on p1.userID=p2.userID and p1.picture<>p2.picture
  left join pictures p3
  on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {

    $stmt->bind_param("i", $first); 
    $stmt->execute();
    $stmt->bind_result($user, $pic1, $pic2, $pic3);
    $stmt->fetch();
    $stmt->close();
}
$mysqli->close();
?>
<div style="position:absolute; top:50px; left:100px; width:800px; text-align: center;">
  <img src="<?PHP echo (isset($pic1) ? $image_path.$pic1 : $no_image); ?>" width="176px" height="197px">
  <img src="<?PHP echo (isset($pic2) ? $image_path.$pic2 : $no_image); ?>" width="176px" height="197px">
  <img src="<?PHP echo (isset($pic3) ? $image_path.$pic3 : $no_image); ?>" width="176px" height="197px">
</div>

我想知道如何在此查询中也获得 pictureID?例如,如果我使用图片进行投票,那么我还必须获取 pictureID。

Supericy 帮助后的回答:(如果有人正在寻找相同的)

if ($stmt = $mysqli->prepare("SELECT p1.userID, p1.picture as pic1, p1.pictureID as pic1id, p2.picture as pic2, p2.pictureID as pic2id, p3.picture as pic3, p3.pictureID as pic3id
FROM
  pictures p1 left join pictures p2
  on p1.userID=p2.userID and p1.picture<>p2.picture
  left join pictures p3
  on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID
LIMIT ?,1")) {

然后绑定(bind)

$stmt->bind_result($user, $pic1, $pic1id, $pic2, $pic2id, $pic3, $pic3id);

最佳答案

试试这个,

SELECT p1.userID,coalesce(p1.pictureID,p2.pictureID,p3.pictureID) pictureID, 
       coalesce(p1.picture,p2.picture,p3.picture) as pic
FROM
  pictures p1 left join pictures p2
  on p1.userID=p2.userID and p1.picture<>p2.picture
  left join pictures p3
  on p1.userID=p3.userID and p1.picture<>p3.picture and p2.picture<>p3.picture
GROUP BY p1.userID,pictureID,pic
LIMIT ?,1

关于php - mysqli 获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14026727/

相关文章:

php - Laravel 验证规则 : required_without

使用 php 变量作为 id 的 javascript 切换函数

php - 给出警告 : in_array() expects parameter 2 to be array, null

mysql 匹配通配符

mysql - 将几何图形从 MSSQL 导入 MySQL(线串)

php - 使用 php 将 mysql 日期(日期时间)转换为更好的日期格式

php - 包含存储为 blob 的图像的 JSON 数组

php - 无法使用mysqli从数据库中获取数据

php - 使用 MySQL 和 PHP 为我的电子商务网站显示数据

php - Mysql 到 mysqli php