$_SESSION and database records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

$_SESSION and database records

Post by paomoca »

I'm having the hardest time trying to figure out how to get into a session a certain record from a database. I have a while loop and it prints all the database information right, but when I try to get a certain record into a $_SESSION it automatically gives me the last record inserted on the database. =s I hope I explained myself correctly I'm really struggling here... thanks
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SESSION and database records

Post by AbraCadaver »

paomoca wrote:I'm having the hardest time trying to figure out how to get into a session a certain record from a database. I have a while loop and it prints all the database information right, but when I try to get a certain record into a $_SESSION it automatically gives me the last record inserted on the database. =s I hope I explained myself correctly I'm really struggling here... thanks
Obviously you're not doing it properly.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

Re: $_SESSION and database records

Post by paomoca »

yeah I kinda figured that out already ... is there anyway you could show me an example of the proper way to do it?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: $_SESSION and database records

Post by AbraCadaver »

paomoca wrote:yeah I kinda figured that out already ... is there anyway you could show me an example of the proper way to do it?
That was kind of a hint to post some code. Also, I have no idea what you mean by a certain record. Here is the only example I can think of, but it doesn't make much sense really:

Code: Select all

session_start();

while($row = mysql_fetch_assoc($result)) {
   $rows[] = $row;
}
$_SESSION['something'] = $rows[5];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

Re: $_SESSION and database records

Post by paomoca »

Code: Select all

<?php
				while($row = mysql_fetch_array($sqll)) {
			?>
			
				<?php $url = $row['url'];
				$type = $row['type'];
$_SESSION['url'] = $url;
$_SESSION['type'] = $type;
				?>
                <tr>
						
					<td><?php echo htmlentities($row['name']); ?></td>
					<td><?php echo htmlentities($row['description']); ?></td>
					
				
			  <td>
						
							<a href="SUA.php" target="_blank">
								<?php echo $file_download; ?>
							</a>
						
					</td>
<td><?php echo $url;?></td>
					<td width="10%"><?php echo $type;?></td>
[text]what I'm looking for is to use those two sessions on SUA.php. When I echo $type and $url I get the right records according to the row but when I use the session on SUA.php I get the last record.... I hope I'm making any sense here I don't really speak English jeje please bear with meee=) [/text]
Last edited by paomoca on Thu Jul 21, 2011 1:08 pm, edited 1 time in total.
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

Re: $_SESSION and database records

Post by paomoca »

depending on the row I clic the link in I want the $_SESSION to have the values of that row... make any sense??
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: $_SESSION and database records

Post by Weirdan »

If you need a php script to receive different parameters depending on the link clicked the most logical way would be to pass required parameters via GET.
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

Re: $_SESSION and database records

Post by paomoca »

could you show me an examplee?? please
User avatar
Pazuzu156
Forum Contributor
Posts: 241
Joined: Sat Nov 20, 2010 9:00 pm
Location: GA, USA
Contact:

Re: $_SESSION and database records

Post by Pazuzu156 »

Maybe this example will help you.

Link:

Code: Select all

<a href="http://mydomain.com/index.php?session=mysession" name="mysession">My Link</a>
PHP:

Code: Select all

<?php
    // include the file you connected to the db with
    require './assets/connect.php';
    
    // get the information sent over from the link
    $mysession = $_GET['session'];
    
    // check if the session is retrieved from the url via the link clicked
    if(isset($mysession)) {
        // use a mysql query to retrieve the session from the database
        $getsession = mysql_query("SELECT `session` FROM `session_table` LIMIT 1") or die(mysql_error());
        // check if the session from the link matches the session from the database
        // if it doesn't, then disreguard the session
        $rows = mysql_num_rows($getsession);
        if($rows != 1) {
            // loop through the sessions
            while($row = mysql_fetch_assoc($getsession) {
                // set var to get value from the `session` cell of the table
                $session = $row['session'];
                // set the session based on the session pulled from the table
                $_SESSION['my_session'] = $session;
            }
        } else {
            echo 'No Match';
        }
    }
?>
This is more than likely not 100% yours, but it's a very good window to show how something like that can be achieved. I hope this snippet helps you.
- Kaleb Klein
------------------------------------
Web Developer | Software Developer
https://kalebklein.com
PGP Key: https://keybase.io/pazuzu156
paomoca
Forum Newbie
Posts: 6
Joined: Thu Jul 21, 2011 10:00 am

Re: $_SESSION and database records

Post by paomoca »

thanks I'll try that It's from a free script I got I'm just trying to edit it to fit my needs =)
Post Reply