# PHP - Setting session variables from database records?



## Akumos (Jun 8, 2012)

Hi - How would I do this? The connection code works, but it won't set the session variables...

Thanks!


```
<?

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 (Jun 8, 2012)

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.)


----------



## Akumos (Jun 8, 2012)

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!


----------



## caleb (Jun 8, 2012)

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


----------



## caleb (Jun 8, 2012)

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 (Jun 8, 2012)

caleb said:


> 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:


```
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.




caleb said:


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





caleb said:


> 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.


----------

