• We've upgraded our forums. Please post any issues/requests in this thread.

PHP - Setting session variables from database records?

Joined
Nov 30, 2008
Messages
555 (0.17/day)
Likes
28
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
#1
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
10,404 (4.84/day)
Likes
5,484
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#2
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.17/day)
Likes
28
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
#3
:) 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,561 (0.32/day)
Likes
207
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
#4
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,561 (0.32/day)
Likes
207
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
#5
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
10,404 (4.84/day)
Likes
5,484
Location
Concord, NH
System Name Kratos
Processor Intel Core i7 3930k @ 4.2Ghz
Motherboard ASUS P9X79 Deluxe
Cooling Zalman CPNS9900MAX 130mm
Memory G.Skill DDR3-2133, 16gb (4x4gb) @ 9-11-10-28-108-1T 1.65v
Video Card(s) MSI AMD Radeon R9 390 GAMING 8GB @ PCI-E 3.0
Storage 2x120Gb SATA3 Corsair Force GT Raid-0, 4x1Tb RAID-5, 1x500GB
Display(s) 1x LG 27UD69P (4k), 2x Dell S2340M (1080p)
Case Antec 1200
Audio Device(s) Onboard Realtek® ALC898 8-Channel High Definition Audio
Power Supply Seasonic 1000-watt 80 PLUS Platinum
Mouse Logitech G602
Keyboard Rosewill RK-9100
Software Ubuntu 17.10
Benchmark Scores Benchmarks aren't everything.
#6
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: