Solved

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

Posted on 2012-03-28
9
490 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 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
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.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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…
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 …

630 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