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

x
?
Solved

MySQL separate result rows in PHP to store as separate session variables

Posted on 2012-03-28
9
Medium Priority
?
491 Views
Last Modified: 2012-03-28
I have a MySQL query that always outputs 10 result rows.

see attached code snippet for my query.

I need to pass the cell value from the first column (articleID) of each result row into distinct different session variables for use in other pages:

$_SESSION['var_01_articleID']
$_SESSION['var_02_articleID']
$_SESSION['var_03_articleID']
$_SESSION['var_04_articleID']
$_SESSION['var_05_articleID']
$_SESSION['var_06_articleID']
$_SESSION['var_07_articleID']
$_SESSION['var_08_articleID']
$_SESSION['var_09_articleID']
$_SESSION['var_10_articleID']

How do i separate out the result values and place them in these session variables using PHP? Its the separating and placing that I am concerned about.
$query_rs_last10 = "select articleID, subject, body, extendedBody, image, publishDate from article order by publishDate DESC limit 0,10";
$rs_last10 = mysql_query($query_rs_last10, $pseka) or die(mysql_error());
$row_rs_last10 = mysql_fetch_assoc($rs_last10);
$totalRows_rs_last10 = mysql_num_rows($rs_last10);

Open in new window

0
Comment
Question by:marcoullis
[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
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:Chris Sandrini
ID: 37775562
Not quite sure what you mean. Can you show us some output? Like how does the sql result look like?
0
 

Author Comment

by:marcoullis
ID: 37775582
row   articleID
 1      12336
 2      12335
 3      12334
 4      12333

and so on...

I want to store

12336 in $_SESSION['var_01_articleID']
12335 in $_SESSION['var_02_articleID']
12334 in $_SESSION['var_03_articleID']
12333 in $_SESSION['var_04_articleID']

and so on...

Is that better?
0
 
LVL 11

Accepted Solution

by:
Chris Sandrini earned 2000 total points
ID: 37775667
something like

$count = 1
while ($row = mysql_fetch_assoc($rs_last10))
{
    $_SESSION['var_'.$count.'_articleID'] = $row['articleID'];
    $count++;
}

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

Open in new window


I did not test the output. Might have a typo in. But you get the idea.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:marcoullis
ID: 37775932
<?php
                                    
                                    $query_rs_last10 = "select articleID, subject, body, extendedBody, image, publishDate from article order by publishDate DESC limit 0,10";
                                    $rs_last10 = mysql_query($query_rs_last10, $pseka) or die(mysql_error());
                                    $row_rs_last10 = mysql_fetch_assoc($rs_last10);
                                    $totalRows_rs_last10 = mysql_num_rows($rs_last10);
                                    
                                    $count = 0;
                                    while ($row = mysql_fetch_assoc($rs_last10))
                                    {
                                    $_SESSION['var_'.$count.'_articleID'] = $row['articleID'];
                                    $count++;
                                    }
                                    
                                    echo "<pre>";
                                    print_r($_SESSION);
                                    echo "</pre>";
                                    
                                    ?>

Open in new window


I have embedded what worked for me... only minor changes on what you posted.

thx.

:-)

-hellenica
0
 

Author Comment

by:marcoullis
ID: 37775943
Quick question it repeats two of the values

Array
(
    [var_articleID] => 9356
    [var_1_articleID] => 12334
    [var_2_articleID] => 12333
    [var_3_articleID] => 12332
    [var_4_articleID] => 12331
    [var_5_articleID] => 12330
    [var_6_articleID] => 12329
    [var_7_articleID] => 12328
    [var_8_articleID] => 12327
    [var_9_articleID] => 12327
    [var_0_articleID] => 12335
)

var 8 and 9 for some reason.

any idea why?

-hellenica
0
 

Author Comment

by:marcoullis
ID: 37775977
Also its skipping the first record, which should be 12336, not 12335.

Again, any ideas?

-hellenica
0
 
LVL 11

Expert Comment

by:Chris Sandrini
ID: 37775988
you might have 2 entries in the db with the same id.
0
 

Author Comment

by:marcoullis
ID: 37776006
no i don't.

12336 is the first key that results from the query, but it never shows up in the session variables. shows up just fine elsewhere on the page. no matter what i do i can't get id 12336 to set as a session variable.

any other ideas?

-hellenica
0
 

Author Comment

by:marcoullis
ID: 37776028
<?php
                                    
                                    
$query_rs_last10 = "select articleID, subject, body, extendedBody, image, publishDate from article order by publishDate DESC limit 11";
$rs_last10 = mysql_query($query_rs_last10, $pseka) or die(mysql_error());
$row_rs_last10 = mysql_fetch_assoc($rs_last10);
$totalRows_rs_last10 = mysql_num_rows($rs_last10);
                                    
																		
                                    $count = 1;
                                    while ($row = mysql_fetch_assoc($rs_last10))
                                    {
                                    $_SESSION['var_'.$count.'_articleID'] = $row['articleID']+1;
                                    $count++;
                                    }
                                    
                                    echo "<pre>";
                                    print_r($_SESSION);
                                    echo "</pre>";
                                    
                                    ?>

Open in new window


never mind, found the culprit.

i attached the solution.



-hellenica
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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