Solved

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

Posted on 2012-03-28
9
481 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
  • 6
  • 3
9 Comments
 
LVL 11

Expert Comment

by:un1x86
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:
un1x86 earned 500 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
 

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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:un1x86
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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.

762 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

20 Experts available now in Live!

Get 1:1 Help Now