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

PHP - Setting session variables from database records?

Discussion in 'Programming & Webmastering' started by Akumos, Jun 8, 2012.

  1. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    535 (0.25/day)
    Thanks Received:
    27
    Location:
    Birmingham, England...
    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"];
          
        }
          
    ?>
     
  2. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,273 (6.49/day)
    Thanks Received:
    2,081
    Location:
    Concord, NH
    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 says thanks.
  3. Akumos

    Akumos

    Joined:
    Nov 30, 2008
    Messages:
    535 (0.25/day)
    Thanks Received:
    27
    Location:
    Birmingham, England...
    :) 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!
     
  4. caleb

    Joined:
    Sep 15, 2004
    Messages:
    1,540 (0.42/day)
    Thanks Received:
    204
    Location:
    Poland,Slask
    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());
     
  5. caleb

    Joined:
    Sep 15, 2004
    Messages:
    1,540 (0.42/day)
    Thanks Received:
    204
    Location:
    Poland,Slask
    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.
     
  6. Aquinus

    Aquinus Resident Wat-man

    Joined:
    Jan 28, 2012
    Messages:
    6,273 (6.49/day)
    Thanks Received:
    2,081
    Location:
    Concord, NH
    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.


    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.
    Just because a query returns no rows doesn't mean it threw an error.
     
    Last edited: Jun 8, 2012

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page