Jump to content

Using Session()


nishmgopal

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/
Share on other sites

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']) . "'";

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780222
Share on other sites

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());

 

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780229
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780250
Share on other sites

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)

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780259
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780266
Share on other sites

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";

}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/148581-using-session/#findComment-780273
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.