我试图在我的主页上显示我的数据库中的随机发布,但是当我插入代码时没有显示任何内容。我如何获得以下代码来显示我的db中的随机行?
<?php
require "connect2.php";
$sql = "SELECT * FROM tablename
ORDER BY RAND()";
while($row = mysql_fetch_array($sql)){
$userid = $row["userid"];
$user = $row["user"];
$city = $row["city"];
$desc = $row["description"];
$title = $row["title"];
$state = $row["state"];
echo"<h3><font face='helvetica'><font size='4'><b><font color='B80000'>$title</font></font></font></b> <font color='A0A0A0'><a href='profile.php?id=$userid'>$user</a></font>
<font face='helvetica'><font size='3'><br> $desc</font></font><br>
<h3><font color='101010'> $city,$state <font color='A0A0A0'>$date</font> </font><a href='bid.php?id=$userid'>Bids</a>";
?>
答案 0 :(得分:1)
您没有执行查询。您只是创建一个恰好包含SQL语句的变量。这就像说$win = 'Win the lottery';
并希望您的银行帐户存入非常大的数字。
$sql = 'SELECT ...';
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$somedata = $row['somefield_from_your_database'];
echo "The value is $somedata";
是您需要的基本代码序列。
答案 1 :(得分:0)
也许你应该执行你的查询:
<?php
require "connect2.php";
$sql = "SELECT * FROM tablename ORDER BY RAND()";
$result = mysqli_query($dbc,$query);
if
(mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_object($result)) {
$html= <<<HTML
<h3><font face='helvetica'><font size='4'><b><font color='B80000'>$title</font></font></font></b> <font color='A0A0A0'><a href='profile.php?id=$userid'>$row->user</a></font>
<font face='helvetica'><font size='3'><br> $row->desc</font></font><br>
<h3><font color='101010'> $city,$state <font color='A0A0A0'>$row->date</font> </font><a href='bid.php?id=$row->userid'>Bids</a>
HTML;
echo ($html);
}
}
答案 2 :(得分:0)
<?
require_once("connect2.php");
$sql = "SELECT * FROM tablename ORDER BY RAND()";
while( $row = mysql_fetch_array($sql) )
{
$userid = $row["userid"];
$user = $row["user"];
$city = $row["city"];
$desc = $row["description"];
$title = $row["title"];
$state = $row["state"];
if (!$firstname)
{
$firstname = $username;
}
echo "<h3><font face='helvetica'><font size='4'><b><font color='B80000'>$title</font></font></font></b> <font color='A0A0A0'><a href='profile.php?id=$userid'>$user</a></font><font face='helvetica'><font size='3'><br> $desc</font></font><br><h3><font color='101010'> $city,$state <font color='A0A0A0'>$date</font> </font><a href='bid.php?id=$userid'>Bids</a>";
}
?>
答案 3 :(得分:0)
select top 10 * from [tablename] order by newid()
使用此查询可获得随机记录...