Jump to content

[SOLVED] How To Display Random Data Table Row


phpQuestioner

Recommended Posts

I would like to be able to display a random database row in an ad system I am thinking about developing.

 

Here is MySQL code that I create:

 

<?php
mysql_connect("localhost","username","password"); 
mysql_select_db("databaseName");
mysql_query("SELECT * FROM dateTableName ORDER BY RAND() LIMIT 1");
?>

 

Is this correct and what would be my next step to make a single random row display. I am sure I would use echo or print the database variables in some fashion. I the past I have been using the mysql_fetch_array method, but I am not quit sure how to do this now.

Do it something like this...

 

$result = mysql_query($query) or die(mysql_error());


echo "<table class='text' align=Center CELLSPACING='0'BORDERCOLOR='#007FFF' BGCOLOR='cornsilk' border='1'>



<tr><th colspan=12><Font face='Verdana'size='3'>Official Receipt</th></Font>
<tr>

<th><Font size='1'>Act Date</th></Font>

</tr></tr>";while($row = mysql_fetch_array($result))
  {



  echo "<tr>";

  
  echo "<td>" . $row['formatteddate1'] . "</td></font>"; 
  
  
  echo "</tr>";
  }
echo "</table>";mysql_close($mysql);

 

Change table columns and other data according to ur need.

Hope this will help u.

 

Thanks mmarif4u, but I finally figured out how to accomplish this; my code is below:

 

<?php

mysql_connect ("localhost", "username", "password");

mysql_select_db ("databaseName");

$content = mysql_query("SELECT * FROM dataTableName ORDER BY RAND() LIMIT 1");

$row = mysql_fetch_row($content);

// $row[number] is based on the structure of your data table from top to bottom
// $row[0] is equal the first field in your data table

echo $row[1];
echo " ";
echo $row[2];
echo " ";
echo $row[3];
echo " ";
echo $row[4];

?> 

Thanks mmarif4u, but I finally figured out how to accomplish this; my code is below:

 

<?php

mysql_connect ("localhost", "username", "password");

mysql_select_db ("databaseName");

$content = mysql_query("SELECT * FROM dataTableName ORDER BY RAND() LIMIT 1");

$row = mysql_fetch_row($content);

// $row[number] is based on the structure of your data table from top to bottom
// $row[0] is equal the first field in your data table

echo $row[1];
echo " ";
echo $row[2];
echo " ";
echo $row[3];
echo " ";
echo $row[4];

?> 

 

R u check ur code is it working, what u want from it.

yeah it works fine.

 

the purpose of this code is to be able to display a random row's content with as many fields as I choose from one specific row of my data table.

 

say I have several items for sale and I want to randomly display the: picture, description, and price of each item in an advertisement. If structure a data table row's fields like this:

 

                          picture                    (this equals $row[0])

                          description                (this equals $row[1])

                          price                        (this equals $row[2])

                          hyperlink                  (this equals $row[3])   

 

I can use my code to pull a random items that I have for sale, that are in my database.

 

 

Code Example

 

<?php

mysql_connect ("localhost", "username", "password");

mysql_select_db ("databaseName");

$content = mysql_query("SELECT * FROM dataTableName ORDER BY RAND() LIMIT 1");

$row = mysql_fetch_row($content);

// $row[number] is based on the structure of your data table from top to bottom
// $row[0] is equal the first field in your data table

echo "<a href=\"";
echo $row[3];
echo "\">";
echo $row[0];
echo " ";
echo $row[1];
echo " ";
echo $row[2];
echo "</a>";

?> 

 

With this every time a the page reloads I will get a random item for sale that has been pulled from my database's data 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.