开发者

How do I display this result set - it is not working [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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> &nbsp;<font color='A0A0A0'><a href='profile.php?id=$userid'>$user</a></font>
<font face='helvetica'><font size='3'><br>&nbsp;$desc</font></font><br>

   <h3><font color='101010'> &nbsp;$city,$state&nbsp;<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> &nbsp;<font color='A0A0A0'><a href='profile.php?id=$userid'>$row->user</a></font>
    <font face='helvetica'><font size='3'><br>&nbsp;$row->desc</font></font><br>

   <h3><font color='101010'> &nbsp;$city,$state&nbsp;<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> &nbsp;<font color='A0A0A0'><a href='profile.php?id=$userid'>$user</a></font><font face='helvetica'><font size='3'><br>&nbsp;$desc</font></font><br><h3><font color='101010'> &nbsp;$city,$state&nbsp;<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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜