• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

PHP - Setting session variables from database records?

Joined
Nov 30, 2008
Messages
555 (0.10/day)
Location
Birmingham, England...
Processor Intel Core 2 Quad Q6600 @ 2.8GHz
Motherboard Gigabyte X48T-DQ6
Cooling Zalman 9500 LED CPU Cooler
Memory 2x 2GB Corsair DDR3 XMS3 DHX - 1600MH/PC3-12800
Video Card(s) Gigabyte HD4870 1GB
Storage 2x Seagate 320GB Barracuda (RAID 0) 3x 1TB Samsung F3, 140GB WD Maxtor (10,000rpm)
Display(s) 2x 20" LG Flatron L204WS
Power Supply Powercool 850W
Software Windows 7 Ultimate x64
Hi - How would I do this? The connection code works, but it won't set the session variables...

Thanks!

PHP:
<?

session_start();

	  $_SESSION['name'];
	  $_SESSION['email'] = $_POST['email'];;
	  $_SESSION['age'];

	  
	  $con = mysql_connect("localhost","admin","pass");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("a8429525_game", $con);

 $sql ="SELECT * FROM user";
 $sql.=" WHERE email=\"".$_SESSION['email']."\"";	

$queryResult=mysql_query($sql);
    
    if (mysql_error())
    {
      echo "Problem with Query<BR>";
      echo "The following error message was returned from MySQL:<BR>";
      echo mysql_error();
      exit;
    }
	
	while ($dbRecord=mysql_fetch_array($queryResult))
	{
   
		
	  $_SESSION['name'] = $dbRecord["name"];
	  $_SESSION['age'] = $dbRecord["age"];
	  
	}
	  
?>
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
If you're not getting any errors back, it sounds like the query might not be returning any rows. After the while() statement did you try to `var_dump($dbRecord);` to see what is actually in it? If nothing gets printed, the very first call to mysql_fetch_array() may be returning false (no rows.)
 
Joined
Nov 30, 2008
Messages
555 (0.10/day)
Location
Birmingham, England...
Processor Intel Core 2 Quad Q6600 @ 2.8GHz
Motherboard Gigabyte X48T-DQ6
Cooling Zalman 9500 LED CPU Cooler
Memory 2x 2GB Corsair DDR3 XMS3 DHX - 1600MH/PC3-12800
Video Card(s) Gigabyte HD4870 1GB
Storage 2x Seagate 320GB Barracuda (RAID 0) 3x 1TB Samsung F3, 140GB WD Maxtor (10,000rpm)
Display(s) 2x 20" LG Flatron L204WS
Power Supply Powercool 850W
Software Windows 7 Ultimate x64
:) Thank you for your reply... the email address was wrong in the table so, as you suggested, no rows were being returned.

You have introduced me to a level of testing :)

thanks again!
 
Joined
Sep 15, 2004
Messages
1,583 (0.22/day)
Location
Poland,Slask
System Name HAL
Processor Core i5 2500K
Motherboard Asus P8P67 Pro Rev3.1
Cooling stock
Memory 2x4GB Kingston 1600Mhz Blu
Video Card(s) Asus 560Ti DirectCuII TOP
Storage Kingston 120 3K SSD,WD Black WD1502FAEX
Display(s) LG 1440x900
Case Chieftec Mesh Midi
Audio Device(s) onboard
Power Supply Corsair TX750V2
Software w8
from php doc mysql_error does not return boolean
Returns the error text from the last MySQL function, or '' (empty string) if no error occurred.

What i always did is use the error in conjunction with mysql_query like this :
$result = mysql_query($query) or die(mysql_error());
 
Joined
Sep 15, 2004
Messages
1,583 (0.22/day)
Location
Poland,Slask
System Name HAL
Processor Core i5 2500K
Motherboard Asus P8P67 Pro Rev3.1
Cooling stock
Memory 2x4GB Kingston 1600Mhz Blu
Video Card(s) Asus 560Ti DirectCuII TOP
Storage Kingston 120 3K SSD,WD Black WD1502FAEX
Display(s) LG 1440x900
Case Chieftec Mesh Midi
Audio Device(s) onboard
Power Supply Corsair TX750V2
Software w8
PS. I see they are suggesting to move out to PDO from regular query stuff nowdays. I'd go with their suggestions as they move PHP development very quickly.
 

Aquinus

Resident Wat-man
Joined
Jan 28, 2012
Messages
13,147 (2.94/day)
Location
Concord, NH, USA
System Name Apollo
Processor Intel Core i9 9880H
Motherboard Some proprietary Apple thing.
Memory 64GB DDR4-2667
Video Card(s) AMD Radeon Pro 5600M, 8GB HBM2
Storage 1TB Apple NVMe, 4TB External
Display(s) Laptop @ 3072x1920 + 2x LG 5k Ultrafine TB3 displays
Case MacBook Pro (16", 2019)
Audio Device(s) AirPods Pro, Sennheiser HD 380s w/ FIIO Alpen 2, or Logitech 2.1 Speakers
Power Supply 96w Power Adapter
Mouse Logitech MX Master 3
Keyboard Logitech G915, GL Clicky
Software MacOS 12.1
PS. I see they are suggesting to move out to PDO from regular query stuff nowdays. I'd go with their suggestions as they move PHP development very quickly.

I typically use PostgteSQL and I have a web framework I've been developing which is soon to be used in production where I work. It simplifies a lot of things, such as not having to worry about connecting to the database or handling errors, because the database abstraction layer handles that. I don't know about MySQLi, but PostgreSQL will tell you what a query has returned.

So in what I've written (using postgresql,) it would look like this:

Code:
class SomeNewController {
    public function index() {
        $db = \Plum\DB::get_conn();
        $rec = $db->select('users', array('email' => \Plum\HTTP::input('email')), 1);
        // Or: $rec = $db->select_sql('SELECT * FROM {users} WHERE email = ?', array(\Plum\HTTP::input('email')), 1);
        if(!empty($rec)) {
            \Plum\Session::set('email', $rec->email);
        }
    }
}

I would have used php tags, but for some reason wrapping php code with namespacing in PHP tags strips the backslashes out, which are needed to determine namespace scope.


from php doc mysql_error does not return boolean


What i always did is use the error in conjunction with mysql_query like this :
$result = mysql_query($query) or die(mysql_error());

PS. I see they are suggesting to move out to PDO from regular query stuff nowdays. I'd go with their suggestions as they move PHP development very quickly.

No, but it returns a type that PHP interprets as a false, but that isn't what I was talking about. mysql_fetch_assoc says this in php.net's doc.
PHP.net said:
Returns an associative array of strings that corresponds to the fetched row, or FALSE if there are no more rows.

Just because a query returns no rows doesn't mean it threw an error.
 
Last edited:
Top