Store Mysql search results in a Session?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Store Mysql search results in a Session?

Post by drummond40 »

I have tried this for days now and I'm having no luck, I assume this is possible?

I have a SELECT statement that displays the results in a table on a page.
I want the user to be able to select numerous results via checkboxs and have these checked results stored in a $_session and passed to another page.

My Select statement works fine, the results are displayed and checkboxes are displyed for each result.
Is there anyway I can store the checked results in a php session?

Thank you.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Store Mysql search results in a Session?

Post by Bill H »

Yes, there is. PHP sessions are simply an array named $_SESSION. The only tricky part is that session_start() must be called at the beginning of each script using that array. You may want to search for tutorials on sessions.
drummond40
Forum Newbie
Posts: 8
Joined: Sun Aug 17, 2008 2:21 am

Re: Store Mysql search results in a Session?

Post by drummond40 »

Hi thanks for the reply, I've tried many tutorials all of which don't relate to passing SQL results in a Session.
I have used Sessions before but not with SQL I thought it would be the same process. Below is what I have so far.

Page1.php

Code: Select all

 
<?php
session_start();
$_SESSION["$row"] = "name";
// Make a MySQL Connection
mysql_connect("localhost", "????", "????") or die(mysql_error());
mysql_select_db("????") or die(mysql_error());
$result = mysql_query("SELECT name FROM customer WHERE name LIKE '$value%' ") 
or die(mysql_error()); 
if(isset($_GET['value']))
{
$value = $_GET['value'];
}
else
{
$value = '';  // set a default value to be sure
}
//echo a table
echo "<center><table border='0'>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
    echo "<tr><td>"; 
    echo $row["name"];
    echo "</td>";
echo "<td>";
print "<td><form><input type=\"checkbox\" value= $row name=\"check[]\"></form></td>\n";
echo "</td>";
    echo "</tr>";
} 
echo "</table></center>";
?>
 
Page2.php

Code: Select all

 
<?
session_start();
echo "Your chosen names are:";
echo $_SESSION["$row"];
?>
 
Page2.php only displays the word 'name' but I want it to display the results from the feild 'name' that are displayed on page1.php
Can you see what I have done wrong? or point in the right direction?

Cheers.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Store Mysql search results in a Session?

Post by Bill H »

Well, I'm not sure of the overall purposeor flow of your script, but:

Code: Select all

 
$result = mysql_query("SELECT name FROM customer WHERE name LIKE '$value%' ")
$row = mysql_fetch_array($result);
$_SESSION['name'] = $row['name'];
 
is basically how you put the content of the database into the session array.
Post Reply