nishmgopal Posted March 9, 2009 Share Posted March 9, 2009 Hi guys I am trying to pass a variable across pages and I believe using sessions is probably the best way. The code on the page where the variable is based is shown below. the variable 'job' is from a form. <?php session_start(); $_SESSION['job'] = $_POST['job']; ?> to read the variable on other pages I am using: <?php session_start(); $getjob = $_SESSION['job']; ?> And I am trying to use it in the following SQL query: $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_POST['job']) . "'"; When I execute this query I get a blank screen with no errors! Please help Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/ Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 You are using the POST array and not the SESSION $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_POST['job']) . "'"; $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_SESSION['job']) . "'"; Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780222 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 I have changed it to what you suggested but again, it just returns a blank screen. No errors. Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780225 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 print the query to the screen to make sure it looks ok. Die if the query produces an error: // does the query look ok - if ok then comment out and run the query below print $query1; // run the query $result = mysql_query($query1) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780229 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 I ran this query: <?php session_start(); $getjob = $_SESSION['job']; ?> <?php $host="co-project"; $user="conmg"; $password="********"; $conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql'); $dbname="conmg"; $dbname=@mysql_select_db($dbname, $conn) or die("Couldn't select database"); //First Table Queries $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_SESSION['job']) . "'"; // does the query look ok - if ok then comment out and run the query below print $query1; // run the query //$result = mysql_query($query1) or die(mysql_error()); ?> And I got a blank screen. Then I ran this: <?php session_start(); $getjob = $_SESSION['job']; ?> <?php $host="co-project"; $user="conmg"; $password="rey38pgb"; $conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql'); $dbname="conmg"; $dbname=@mysql_select_db($dbname, $conn) or die("Couldn't select database"); //First Table Queries $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='" . mysql_real_escape_string($_SESSION['job']) . "'"; // does the query look ok - if ok then comment out and run the query below //print $query1; // run the query $result = mysql_query($query1) or die(mysql_error()); ?> And again I got a blank screen Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780250 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 Firstly you do not need to open and close php tags like you are. ?> <?php The fact that you are getting a blank screen suggests there is an error but you have error reporting disabled. You can change this in your php ini file or add the following to the top of your your script. I would suggest placing this in a common include (included on all pages of your site): ini_set('display_errors', 1); ini_set('error_reporting', E_ALL & ~E_NOTICE); Also the @ on your functions supresses errors. You should get rid of this. @mysql_select_db($dbname, $conn) mysql_select_db($dbname, $conn) Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780259 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 But all my other errors are shown, whenever I've had them. Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780264 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 Does this print the query to the screen? You said you got a blank screen. print $query1; If you get nothing issuing this command then there must be a script error. If you do see the query then all is OK. I cannot see what you are expecting to be on the screen as your code is cut off. Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780266 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 When i print the query, its prints to the screen. the full code is: <? session_start(); $getjob = $_SESSION['job']; ?> $conn = mysql_connect($host, $user, $password) or die ('Error connecting to mysql'); $dbname="conmg"; $dbname=@mysql_select_db($dbname, $conn) or die("Couldn't select database"); ////// $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'"; $result1 = mysql_query($query1) or die ("Couldn't execute query."); $colcnt = 0; while ($row = mysql_fetch_array($result1,MYSQL_ASSOC)) { { if (!$colcnt) { $colcnt = 7; } $colcnt--;{ $Name1 = $row['Project_Name']; $Java1 = $row['Java']; $Leadership1 = $row['Leadership']; $Communication1 = $row['Communication']; $Teamwork1 = $row ['Team_Work']; $Problem_Solving1 = $row['Problem_Solving']; } } echo "$Java1"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780273 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 This is bad syntax. You braces have no apparent function. $colcnt--;{ Also echo "$Java1"; Does not need to be contained with " echo $Java1 Check your code carefully. Is your query returning the correct results? Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780293 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 yes my query is returning proper results when i use a row name such as 'Project_Name='Manager' but if I try to use the variable $getjob, it returns a blank screen! Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780303 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 Do you see the correct query with the correct job id within the query when you print to the screen? print $query1; Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780307 Share on other sites More sharing options...
nishmgopal Posted March 9, 2009 Author Share Posted March 9, 2009 When I print the query: $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='Manager'"; I get the correct results When I print the query: $query1 = "SELECT * FROM Job_Spec WHERE Project_Name ='$getjob'"; SELECT * FROM Job_Spec WHERE Project_Name ='' Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780318 Share on other sites More sharing options...
JonnoTheDev Posted March 9, 2009 Share Posted March 9, 2009 there is your problem. your value is not set in the session. check your code that sets this value. Quote Link to comment https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780323 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.