Jump to content

[SOLVED] 3 random results from one table


CodeMama

Recommended Posts

I have to output 3 random text blurbs from one table and count how many times they appear, I can get one, but everything I have tried to make 3 random results just seems to cause an endless loop and timeout, and I have figured the counting part out yet either (big big newbie here)

here is my code so far:

<?php  include 'inc/dbconnOpen.php';
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

$sql ="select * from textads ORDER BY RAND() LIMIT 0,1";  /* query */
$result= mysql_query($sql);

$num=mysql_num_rows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$href=mysql_result($result,$i,"href");
$blurb=mysql_result($result,$i,"blurb");
$title=mysql_result($result,$i,"title");



        
        
        
echo "<table border= 1 height=90px width=468px bgcolor=#cccccc><tr><td>$title<br>$blurb<br>
<A HREF='$href'>$href</a></td><td>$title<br>$blurb<br>
<A HREF='$href'>$href</a></td><td>$title<br>$blurb<br>
<A HREF='$href'>$href</a></td></tr></table>";

$i++;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/134639-solved-3-random-results-from-one-table/
Share on other sites

um...noticed you have a mysql_close() WAY to early in the script. frankly, you don't need that at all as PHP will do that automatically

 

try:

<?php  include 'inc/dbconnOpen.php';
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

$sql ="select * from textads ORDER BY RAND() LIMIT 0,3";  /* query */
$result= mysql_query($sql);

echo "<b><center>Database Output</center></b><br><br>";
echo "<table border= 1 height=90px width=468px bgcolor=#cccccc><tr>";
while ($row = mysql_fetch_assoc($result)) {
  echo "<td>{$row['title']}<br>{$row['blurb']}<br><A HREF=\"{$row['href']}\">{$row['href']}</a></td>\n";
}
echo "</tr></table>";
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.