close ad
WARNING PC USERS: Do Not Install the DREAMWEAVER CC 2017 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Trying to get a Session variable to equal a Recordset Value

Thread began 2/02/2010 12:20 am by ijk76319396 | Last modified 2/05/2010 5:40 pm by Jason Byrnes | 3116 views | 8 replies |

ijk76319396

Trying to get a Session variable to equal a Recordset Value

I am trying to get a session variable to equal a recordset value, I am writing the variable as

<?php $_SESSION['COMMID']=$_rsPAGES['page_1']; ?>
However the value for it comes up empty when I try to display it.

The query rsPAGES is filtered by a GET value that equals a SEPARATE session variable that I have directly above the first one I typed. It looks like...

<?php $_SESSION['PAGEID']=$_GET['ID']; ?>


Is it possible to do what I am trying to do here. The rsPAGES values work fine when displayed on other parts of the page but when I try to incorporate them into the session variable I get nothing.

Sign in to reply to this post

Jason ByrnesWebAssist

PHP can be tricky about "seeing" the session.


any time you are trying to set or display a session, you should include the following code first to force php to look for the session contents:

php:
if(!session_id()) session_start();




so to set the session use:

php:
<?php 

if(!session_id()) session_start();
$_SESSION['COMMID']=$_rsPAGES['page_1']; 
?>





then to display it use:

php:
<?php 

if(!session_id()) session_start();
echo 
$_SESSION['COMMID']; 
?>




Also, make sure that the code you are using to set the session value is after the code to create the recordset.

Sign in to reply to this post

ijk76319396

I have been working on the solution and found that I needed to add row before the rsPAGEs so its now

<?php 
if(!session_id()) session_start();
$_SESSION['COMMID']=$row_rsPAGES['page_1'];
?>



If you go to web_comicxx.php?ID=6 you can see what I am trying to do. Now that I added that the echo works fine, it is the text directly under the "by Administrator" at the top of the page, however I am not concerned with displaying the text, rather I want to use the COMMID session value to filter the pages by comments in recordset4. So when someone enters a comment in the database the session value is entered into the applicable field, THIS WORKS as far as being entered correctly into the database, now the problem is with the recordset4 which I am trying to filter using the session variable of COMMID so the comment made should only show up on the page it was made on... however it's not working that way, it shows up sometimes on some pages, then not at all, sometimes I have to hit refresh to see anything. Any idea what could be causing this not to work? I am pasting the relevant code.

<?php $_SESSION['PAGEID']=$_GET['ID']; ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$currentPage = $_SERVER["PHP_SELF"];

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
$insertSQL = sprintf("INSERT INTO messages (username, subject, message, sender, senderID) VALUES (%s, %s, %s, %s, %s)",
GetSQLValueString($_POST['hiddenField'], "text"),
GetSQLValueString($_POST['subject2'], "text"),
GetSQLValueString($_POST['message2'], "text"),
GetSQLValueString($_POST['SENDER'], "text"),
GetSQLValueString($_POST['SENDERID'], "text"));

mysql_select_db($database_con2com, $con2com);
$Result1 = mysql_query($insertSQL, $con2com) or die(mysql_error());

$insertGoTo = "../messagesent.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
Sign in to reply to this post

ijk76319396

Had to post the remaining php down here because it was too long...

$colname_Recordset1 = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_Recordset1 = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_Recordset1 = sprintf("SELECT * FROM members WHERE ID = %s", GetSQLValueString($colname_Recordset1, "int"));
$Recordset1 = mysql_query($query_Recordset1, $con2com) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);



mysql_select_db($database_con2com, $con2com);
$query_Recordset3 = "SELECT ID FROM members ORDER BY ID DESC";
$Recordset3 = mysql_query($query_Recordset3, $con2com) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

$maxRows_Recordset4 = 10;
$pageNum_Recordset4 = 0;
if (isset($_GET['pageNum_Recordset4'])) {
$pageNum_Recordset4 = $_GET['pageNum_Recordset4'];
}
$startRow_Recordset4 = $pageNum_Recordset4 * $maxRows_Recordset4;

$colname_Recordset4 = "-1";
if (isset($_SESSION['COMMID'])) {
$colname_Recordset4 = (get_magic_quotes_gpc()) ? $_SESSION['COMMID'] : addslashes($_SESSION['COMMID']);
}
mysql_select_db($database_con2com, $con2com);
$query_Recordset4 = sprintf("SELECT * FROM comments WHERE pageID = %s ORDER BY ID DESC", GetSQLValueString($colname_Recordset4, "text"));
$query_limit_Recordset4 = sprintf("%s LIMIT %d, %d", $query_Recordset4, $startRow_Recordset4, $maxRows_Recordset4);
$Recordset4 = mysql_query($query_limit_Recordset4, $con2com) or die(mysql_error());
$row_Recordset4 = mysql_fetch_assoc($Recordset4);

if (isset($_GET['totalRows_Recordset4'])) {
$totalRows_Recordset4 = $_GET['totalRows_Recordset4'];
} else {
$all_Recordset4 = mysql_query($query_Recordset4);
$totalRows_Recordset4 = mysql_num_rows($all_Recordset4);
}
$totalPages_Recordset4 = ceil($totalRows_Recordset4/$maxRows_Recordset4)-1;

mysql_select_db($database_con2com, $con2com);
$query_rsVALIDATOR = "SELECT ID FROM comments ORDER BY ID DESC";
$rsVALIDATOR = mysql_query($query_rsVALIDATOR, $con2com) or die(mysql_error());
$row_rsVALIDATOR = mysql_fetch_assoc($rsVALIDATOR);
$totalRows_rsVALIDATOR = mysql_num_rows($rsVALIDATOR);

$maxRows_rsPAGES = 1;
$pageNum_rsPAGES = 0;
if (isset($_GET['pageNum_rsPAGES'])) {
$pageNum_rsPAGES = $_GET['pageNum_rsPAGES'];
}
$startRow_rsPAGES = $pageNum_rsPAGES * $maxRows_rsPAGES;

$colname_rsPAGES = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_rsPAGES = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_rsPAGES = sprintf("SELECT * FROM uploads WHERE ID = %s ORDER BY message_id DESC", GetSQLValueString($colname_rsPAGES, "text"));
$query_limit_rsPAGES = sprintf("%s LIMIT %d, %d", $query_rsPAGES, $startRow_rsPAGES, $maxRows_rsPAGES);
$rsPAGES = mysql_query($query_limit_rsPAGES, $con2com) or die(mysql_error());
$row_rsPAGES = mysql_fetch_assoc($rsPAGES);

if (isset($_GET['totalRows_rsPAGES'])) {
$totalRows_rsPAGES = $_GET['totalRows_rsPAGES'];
} else {
$all_rsPAGES = mysql_query($query_rsPAGES);
$totalRows_rsPAGES = mysql_num_rows($all_rsPAGES);
}
$totalPages_rsPAGES = ceil($totalRows_rsPAGES/$maxRows_rsPAGES)-1;

$colname_rsRATINGS = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_rsRATINGS = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_rsRATINGS = sprintf("SELECT * FROM rating WHERE ARTISTusername = %s ORDER BY rating DESC", GetSQLValueString($colname_rsRATINGS, "text"));
$rsRATINGS = mysql_query($query_rsRATINGS, $con2com) or die(mysql_error());
$row_rsRATINGS = mysql_fetch_assoc($rsRATINGS);
$totalRows_rsRATINGS = mysql_num_rows($rsRATINGS);

$colname_rsAVGRATING = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_rsAVGRATING = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_rsAVGRATING = sprintf("SELECT FORMAT(AVG(rating), 2) AS averageRating FROM rating WHERE ARTISTusername = %s", GetSQLValueString($colname_rsAVGRATING, "int"));
$rsAVGRATING = mysql_query($query_rsAVGRATING, $con2com) or die(mysql_error());
$row_rsAVGRATING = mysql_fetch_assoc($rsAVGRATING);
$totalRows_rsAVGRATING = mysql_num_rows($rsAVGRATING);

mysql_select_db($database_con2com, $con2com);
$query_rsADMIN = "SELECT * FROM adminstuff";
$rsADMIN = mysql_query($query_rsADMIN, $con2com) or die(mysql_error());
$row_rsADMIN = mysql_fetch_assoc($rsADMIN);
$totalRows_rsADMIN = mysql_num_rows($rsADMIN);

$colname_rsSCORE = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_rsSCORE = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_rsSCORE = sprintf("SELECT SUM(points) AS sumScore FROM combinedscore WHERE userID = %s", GetSQLValueString($colname_rsSCORE, "text"));
$rsSCORE = mysql_query($query_rsSCORE, $con2com) or die(mysql_error());
$row_rsSCORE = mysql_fetch_assoc($rsSCORE);
$totalRows_rsSCORE = mysql_num_rows($rsSCORE);

$colname_rsSUB = "-1";
if (isset($_SESSION['PAGEID'])) {
$colname_rsSUB = (get_magic_quotes_gpc()) ? $_SESSION['PAGEID'] : addslashes($_SESSION['PAGEID']);
}
mysql_select_db($database_con2com, $con2com);
$query_rsSUB = sprintf("SELECT * FROM subscribe WHERE SUBEEID = %s", GetSQLValueString($colname_rsSUB, "text"));
$rsSUB = mysql_query($query_rsSUB, $con2com) or die(mysql_error());
$row_rsSUB = mysql_fetch_assoc($rsSUB);
$totalRows_rsSUB = mysql_num_rows($rsSUB);

$queryString_Recordset4 = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_Recordset4") == false &&
stristr($param, "totalRows_Recordset4") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_Recordset4 = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_Recordset4 = sprintf("&totalRows_Recordset4=%d%s", $totalRows_Recordset4, $queryString_Recordset4);

$queryString_rsPAGES = "";
if (!empty($_SERVER['QUERY_STRING'])) {
$params = explode("&", $_SERVER['QUERY_STRING']);
$newParams = array();
foreach ($params as $param) {
if (stristr($param, "pageNum_rsPAGES") == false &&
stristr($param, "totalRows_rsPAGES") == false) {
array_push($newParams, $param);
}
}
if (count($newParams) != 0) {
$queryString_rsPAGES = "&" . htmlentities(implode("&", $newParams));
}
}
$queryString_rsPAGES = sprintf("&totalRows_rsPAGES=%d%s", $totalRows_rsPAGES, $queryString_rsPAGES);
?><?php
if(!session_id()) session_start();
$_SESSION['COMMID']=$row_rsPAGES['page_1'];
?>
Sign in to reply to this post

Jason ByrnesWebAssist

You are creating the COMMID session variable after the recordset4 code.


PHP code is executed from top to bottom, for the recordset4 recordset to be able to filter on the COMMID session variable, the COMMID session variable must be created before the recordset4 recordset.

Sign in to reply to this post

ijk76319396

Got it to work, now the only problem is that when I go to a different page the comments show from the previous page I was on, I can get the correct comments but I have to hit refresh, how can I avoid this, do I need some sort of one time autorefresh? What's the best way to do this?

Sign in to reply to this post

Jason ByrnesWebAssist

Most likely it is a code order problem again, but I cant say without seeing the code.


Add the page to a zip archive and attach the zip file so I can see the code.

Sign in to reply to this post

ijk76319396

Attached it.

Attached Files
web_comicXX.zip
Sign in to reply to this post

Jason ByrnesWebAssist

Yeah, so line 256 is where you set the COMMID session variable:
$_SESSION['COMMID']=$row_rsPAGES['page_1'];


your using the rsPAGES recordset which is created at line 153 - 175



you are using it to filter the Recordset4 recordset at lines 121 - 136, since it has not been set yet, it is using the value from the previous page.


you need to re order the code so that
1) the rsPAGES recordset is created
2) the COMMID session variable is set
3) the Recordset4 recordset is created.

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...