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
204 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 110

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 110

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
Myth Busting: MongoDB Scalability (it scales!)

I was talking with one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. He mentioned to me that several customers have been telling him that “MongoDB doesn’t scale!” MongoDB’s scalability was in question?

My response was, “Is that a joke?"

 
LVL 110

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 110

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 110

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 110

Accepted Solution

by:
Ray Paseur earned 500 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 110

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

MongoDB Through a MySQL Lens

This article looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

623 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