Page 1 of 1
$_SESSION and database records
Posted: Thu Jul 21, 2011 10:26 am
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
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 10:33 am
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.
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:02 am
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?
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:07 am
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];
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:29 am
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]
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:37 am
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??
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:41 am
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.
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 11:42 am
by paomoca
could you show me an examplee?? please
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 12:47 pm
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.
Re: $_SESSION and database records
Posted: Thu Jul 21, 2011 1:04 pm
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 =)