Solved

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

Posted on 2012-03-28
9
486 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 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