Solved

Need to load in browser's memory ALL values for ALL the 50 rows of a SELECT * FROM LIMIT 50 Query

Posted on 2009-04-04
14
199 Views
Last Modified: 2013-12-12
I have a query that should be like this

$RESULTS = 50;

SELECT column1,column2,column3..etc..column10  FROM PRODUCTS LIMIT ' .$RESULTS;

And i need to save ALL values for ALL the Rows into ARRAYs (the number of the ROWs to save in memory is equal to $RESULTS)


Thank You
0
Comment
Question by:fario
  • 7
  • 7
14 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
You can use the $_SESSION array to hold this information.  Use session_start() at the top of all pages, and you can have this information available from page to page.
0
 
LVL 1

Author Comment

by:fario
Comment Utility
But how can i store the values in the SESSION array ? Can someone show me a loop to obtain this ?

Anyway i don't need to use those information on a different page,
so maybe should i use several "standard" PHP arrays ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Code something like this...
<?php // RAY_temp_db_2_session.php

session_start();
 

$results = 50;

$sql = "SELECT column1,column2,column3 ...ETC... column10 FROM PRODUCTS LIMIT $results";

$res = mysql_query($sql);

if (!$res) die(mysql_error());
 

while ($row = mysql_fetch_assoc($res))

{

   $_SESSION["db_data"][] = $row;

}

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Let me ask a different question... Why?  What advantage are you trying to obtain by doing this?  There may be a better way to achieve the same result.
0
 
LVL 1

Author Comment

by:fario
Comment Utility
I need to avoid to make 50    SELECT * FROM   queries inside the same page (i'm creating 50 different little flash files; each one reads some parameters PHP will pass to create a different flash animation)

I have 50 DIVs like the following:
<div id="news02">

<?  IF I CAN'T READ ALL THE 50 ROWS IN JUST ONE QUERY, IN THIS PHP SPACE EACH DIV HAVE TO QUERY THE DB TO FIND THE PARAMETERS RELATED TO THE FOLLOWING FLASH FILE   ?> 

        <p>

        <a href="http://get.adobe.com/it/flashplayer" target="_blank">Installare Adobe Flash</a> <a href="https://www.google.com/adsense/support/bin/answer.py?hl=it&answer=12654" target="_blank">e/o attivare Javascript.</a>

        </p>

        </div>

        <script type="text/javascript">

        // <![CDATA[

        var so = new SWFObject("swf/novita/novita.swf", "news02", "180", "210", "9", "#FFFFFF");

        so.addParam("wmode", "transparent");

        so.addParam("movie", "swf/novita/novita.swf");

        so.addParam("quality", "high");

        so.addParam("scale", "noscale");

===>    so.addParam("FlashVars", "<?=PHP SENDS PARAMETERS HERE   ?>"); <===

        so.write("news02");

        // ]]>

        </script>       

Open in new window

0
 
LVL 1

Author Comment

by:fario
Comment Utility
So with the   $_SESSION["db_data"][] = $row;  line i can append ALL the results to the same $_SESSION array ?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Yes, each $row from the data base will be present in $_SESSION["db_data"] and you can find them like this

foreach ($_SESSION["db_data"] as $row)
{
   var_dump($row);
}
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I think you can do the SELECT... LIMIT 50 and then iterate over the results set, creating the flash files as you do the iteration.  Each of the flash file elements might be stored in an array, then at the end of the iterator, you can go over the array to produce the HTML.
0
 
LVL 1

Author Comment

by:fario
Comment Utility
How can i retrieve the values of the $_SESSION["db_data"] stored in the query's row #3

( for example )

How to access to the column1,column2,column3 ...ETC... column10 values of the third Row returned by MySQL ?

Thank You
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
Try this for row 3.  Arrays number from zero, query results number from 1.

$row3 = $_SESSION["db_data"][2]

To see the contents of $row3, use this:
var_dump($row3);

You will see that it is an associative array.  The array keys are the column names from the data base table.  The array values are the values in that row of the data base table.

HTH, ~Ray
0
 
LVL 1

Author Comment

by:fario
Comment Utility
So i have to use this PHP code before every flash DIV ?

$row1 = $_SESSION["db_data"][0];
$price1= $row1['column1'];
$name1 = $row1['column2'];

$row2 = $_SESSION["db_data"][1];
$price2 = $row2['column1'];
$name2 = $row2['column2'];

$row3 = $_SESSION["db_data"][2];
$price3 = $row3['column1'];
$name3 = $row3['column2'];

and so on..  

is this correct
?


0
 
LVL 1

Author Comment

by:fario
Comment Utility
Ok it works thank You
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Rather than using all those individual statements, you might want to use an iterator, such as foreach() to step through the array.  Smaller and more generalized code - less risk of typos.

foreach ($_SESSION["db_data"] as $row)
{
   $price= $row['column1'];
   $name = $row['column2'];
/* PROCESS NAME AND PRICE DATA HERE */
}
0
 
LVL 1

Author Comment

by:fario
Comment Utility
Hi

are You sure the iterator is correct ?
it doesn't pass any value to the vars

Thank you
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now