Solved

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

Posted on 2012-03-28
9
489 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: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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 

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

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.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how 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.
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 look for a specific file type in a local or remote server directory using PHP.

734 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