Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
14 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24068580
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
ID: 24068595
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 111

Expert Comment

by:Ray Paseur
ID: 24068597
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24068602
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
ID: 24068630
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
ID: 24068635
So with the   $_SESSION["db_data"][] = $row;  line i can append ALL the results to the same $_SESSION array ?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24068646
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24068659
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
ID: 24068672
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 111

Accepted Solution

by:
Ray Paseur earned 1500 total points
ID: 24068686
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
ID: 24068728
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
ID: 24068801
Ok it works thank You
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24068811
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
ID: 24071613
Hi

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

Thank you
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 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