-1

I need 3 rows of data from same table using same query on same page but 3 different places. I use the following query 3 times on a single page to get 3 rows of data and shows it on each place . It makes my page load very slow. Is there any way to get 9 rows of data using single query and use it by splitting 3 rows of data at 3 different places on a same page??

Here is my query:

$result = mysql_query("select * from table order by rand() limit 3 ");
while ($row = mysql_fetch_array($result)){
    $a1=$row['a1']; 
    $a2=$row['a2'];
    $a3=$row['a3']; 
    $a4=$row['a4'];
    $a5=$row['a5'];
    echo "<div class=\"col-sm-4\"><div class=\"product-image-wrapper\"><div class=\"single-products\"><div class=\"productinfo text-center\"><a3 src=\"$a3\" alt=\"$a1\"><h2>$a4</h2><h5>$a1</h5><a a2=\"$a2\" target=_blank class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>$a5</a></div></div></div></div>\n";
} 
Subi
  • 107
  • 8
  • If you can, you should [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) [statements](http://php.net/manual/en/pdo.prepared-statements.php) instead, and consider using PDO, [it's really not hard](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Jul 09 '15 at 15:22

1 Answers1

1

Sure, just put your results into an array and then use the array by cherry-picking by index (0-8). Make sure there's enough elements!

$result = mysql_query("select * from table order by rand() limit 9 ");
$nineRows = [];
while ($row = mysql_fetch_array($result)){
  $nineRows[] = $row; 
} 

//first usage
$item = $nineRows[0];
echo "<div class=\"col-sm-4\"><div class=\"product-image-wrapper\"><div class=\"single-products\"><div class=\"productinfo text-center\"><a3 src=\"$item['a3']\" alt=\"$item['a1']\"><h2>$item['a4']</h2><h5>$item['a1']</h5><a a2=\"$item['a2']\" target=_blank class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>$item['a5']</a></div></div></div></div>\n";

//second usage
$item = $nineRows[1];
echo "<div class=\"col-sm-4\"><div class=\"product-image-wrapper\"><div class=\"single-products\"><div class=\"productinfo text-center\"><a3 src=\"$nineRows[1]['a3']\" alt=\"$nineRows[1]['a1']\"><h2>$item['a4']</h2><h5>$item['a1']</h5><a a2=\"$nineRows[1]['a2']\" target=_blank class=\"btn btn-default add-to-cart\"><i class=\"fa fa-shopping-cart\"></i>$item['a5']</a></div></div></div></div>\n";
ptrk
  • 1,800
  • 1
  • 15
  • 24
  • sry sir, i'm new to php and mysql... i can't understand what u r tryng to say me sir... pls hlp me – Subi Jul 09 '15 at 15:35
  • OK, take a look now. It's a bit silly, because of the repeating texts, but I leave that to your learning - the echo should be replaced by a function that takes the $item as a parameter. Otherwise it should work just as well as your example, only giving you access to any of the 'data' items at any point. – ptrk Jul 09 '15 at 16:15