我试图从一列中选择随机行但是我有一个问题,我想选择title != '';
下面没有给我想要的东西的行。我把这and r1.title != '';
显示为空标题。我$row['image_one'];
稍后再附和。我有两行,第一行有 image_one 和 title,第二行有 image_one 而不是 title。它显示带有标题的 image_one,然后当我刷新页面时,我得到空页面。我希望它继续只显示带有标题的图像,
SELECT r1.id, title, price, image_one
FROM table AS r1
JOIN (
SELECT (RAND() * (SELECT MAX(id) FROM table)) AS id
) AS r2
WHERE
r1.id >= r2.id
AND r1.title != ''
ORDER BY r1.id ASC
LIMIT 1
html
<?php include_once("models/config.php");?>
<div class="ho_col_1_pr">
<?php
$stmt = $mydb->prepare("SELECT r1.id, title, price, image_one
FROM hm_table AS r1 JOIN
(SELECT (RAND() *
(SELECT MAX(id)
FROM table)) AS id)
AS r2
WHERE r1.id >= r2.id and r1.title != ''
ORDER BY r1.id ASC
LIMIT 1 ");
$stmt->execute();
if ( false===$stmt ) {
die('prepare() failed: ' . htmlspecialchars($mydb ->error));
}
?>
<?php
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {?>
<div class="pro_img_wrap">
<div class="hid_tit"><div class="hid_tit_ex"><?php echo wordwrap($row['title'], 50, "<br/>\n", true);?></div></div>
<?php
$path = 'images/';
echo "<a href='test.php?redirect=".urlencode($row['title'])." & item=".$row['id']."'>"."<img src='".$path."".$row['image_one']."' style='width:225px;height:169px;'/>"."</a>";?>
</div>
<div class="pr_sep">
<div class="pr_prod">
<?php
var_dump($row['title']) ;
echo $row['price'];?>
</div>
<?php
}
?></div>
</div>