How do I display this result set - it is not working [closed]
Im trying to display random postings from my db on my home page, but nothing displays when I insert my code. How would I get the following code to display random rows from my 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>开发者_JAVA技巧<a href='bid.php?id=$userid'>Bids</a>";
?>
You're not executing your query. You're just creating a variable that happens to contain an SQL statement. it's like saying $win = 'Win the lottery';
and expecting your bank account to get very large numbers deposited into it.
$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";
is the basic sequence of code you need.
Maybe you should execute your query:
<?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);
}
}
<?
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>";
}
?>
select top 10 * from [tablename] order by newid()
by using this query you get random records...
精彩评论