Ruben Martinez Jr.
Ruben Martinez Jr.

Reputation: 3110

PHP/mysql Not Retrieving as Expected

I'm working on a login script in PHP using mysql (yes, I know it's deprecated), however I'm having an issue retrieving information from my database. I have another script that inputs information, and that worked perfectly well, but when it comes to retrieving, it doesn't seem to be working.

When I say it's not working, what I mean is that the retrieve function that I'm using to get information out of the database doesn't seem to be passing information correctly, or perhaps I'm not using what it's passing correctly. Either way, the problem is, it's supposed to retrieve an object of type User which contains an email, password, firstname and lastname.

Below is a simplified version of the PHP file that does all the work, along with the retrieve function that may/may not be the problem. When I run this, all I get is a page that says "First name is" and nothing more, instead of displaying the stored firstname.

As far as troubleshooting:

Here is the code:

<?php
include_once '/home/Databases/User.php';
include_once '/home/Databases/dbUser.php';
include_once '/home/Databases/dbInfo.php';
connect();

$email = mysql_real_escape_string($_GET['email']);
$password = mysql_real_escape_string($_GET['password']);
$rememberme = mysql_real_escape_string($_GET['remember']);

// does this user exist?
$confirm = retrieve_dbUserByEmail($email);
$name = $confirm->get_firstname();

// if they do exist
if($confirm != false) {
    echo "First name is ".$name;
}

// if user doesn't exist
else {
    header('Location: index.php?login=nonexistant');
    exit();
}
?>

And here's the retrieve_dbUserByEmail function:

function retrieve_dbUserByEmail($email){
    connect();
    $email = mysql_real_escape_string($email);
    $query = "SELECT * FROM dbUser WHERE email = '".$email."' LIMIT 1";
    $result = mysql_query($query);
    if ($result==null ) {
       mysql_close();
       return false;
    }
    $result_row = mysql_fetch_assoc($result);
    $user = new User($result_row['id'], $result_row['email'], $result_row['password'], $result_row['firstname'],$result_row['lastname']);
    mysql_close();
    return $user;
}

To answer some questions that have been asked, the retrieve function does NOT return false, escaping the strings only once does not get rid of the error, and the "connect()" function seen connects to the SQL server successfully (verified). Also, if it's useful (though i can't imagine why), I'm posing my User object class below.

<?php
class User {
    private $id;            // Unique User ID
    private $email;         // User Email
    private $password;      // Password
    private $firstname;     // First Name
    private $lastname;      // Last Name

    //Constructor Function
    function __construct($id, $email, $password, $firstname, $lastname) {
        $this->id = $id;
        $this->email = $email;
        $this->password = $password;
        $this->firstname = $firstname;
        $this->lastname = $lastname;
    }

    //Getter Functions
    function get_id() {
        return $this->id;
    }
    function get_email() {
        return $this->email;
    }
    function get_password() {
        return $this->password;
    }
    function get_firstname() {
        return $this->firstname;
    }
    function get_lastname() {
        return $this->lastname;
    }
?>

Upvotes: 2

Views: 121

Answers (2)

Ruben Martinez Jr.
Ruben Martinez Jr.

Reputation: 3110

EDIT:

I figured out the cause of the problem I'm having; The error is in my form. I still haven't quite figured out why it's not working, but for some reason, the form doesn't seem to be POSTing variables correctly upon submission. Thank you to everyone who took a look at my code anyway!

Upvotes: 0

staticsan
staticsan

Reputation: 30555

I would be debugging this by putting a print_r($result_row) just before the new User(...) to confirm that what you think you're getting is what you're actually getting.

I might also be using print_r($confirm) after the return of retrieve_dbUserByEmail() again to confirm that what you think you're getting is what you're actually getting.

Upvotes: 1

Related Questions