-2

Basically, i'm trying to get users data from a database using a class i found, it's parsing all data inside an array as shown here from the following function :

public function Get($field = NULL) {
        if ($field == NULL)
        {
            $data = array();
            while ($row = mysql_fetch_array($this->last_query))
            {
                $data[] = $row;
            }
        } 
        else
        {
            $row = mysql_fetch_array($this->last_query);
            $data = $row[$field];
        }
        return $data;
}

Here's the PHP code i'm using to get the call this function

if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if($_SESSION['csrfToken'] == $_POST['csrfToken']) {
        $email = $_POST['email'];
        $password = $Security->Salt($Security->secParam($_POST['password']));
        $DB->Query("SELECT * FROM `table` WHERE `email` = '$email' AND `password` = '$password'");
        if($DB->num_rows() > 0) {
            $results = $DB->Get();
        } else {
            echo "Account not found";
        }
    }
}

If i do a var_dump on $results it shows the following

array(1) { 
    [0]=> array(8) { 
        [0]=> string(1) "1" ["id"]=> string(1) "1" 
        [1]=> string(35) "[email protected]" ["email"]=> string(35) "[email protected]" 
        [2]=> string(32) "4f14dfef1efe0de64e2b176eac6051cd" ["password"]=> string(32) "4f14dfef1efe0de64e2b176eac6051cd" 
        [3]=> string(1) "1" ["status"]=> string(1) "1" 
    } 
}

how can i access this data ? I've tried calling it by doing the following

$email = $results['email'];
echo $email;

But it's not displaying anything ?

1
  • 2
    Your script is probably vulnerable to SQL injection. Commented Feb 24, 2013 at 16:38

4 Answers 4

2

Even though there's only one result in this instance (I guess?) the array supports multiple.

So find the first result, then take the email from that:

echo $results[0]['email'];
//   ^^^^^^^^^^^
//   first result
Sign up to request clarification or add additional context in comments.

Comments

0

You need to tracking how arrays works. First you have array(1) and then into array another vars such as "email" or 1.

array(1) {  <---- THIS IS ARRAY OCCURED FOR FIRST "0" ARRAY.

What's about

             this
              \/
echo $results[0]["email"]; ?

Comments

-1
if($_SERVER['REQUEST_METHOD'] == 'POST' && $_SESSION['csrfToken'] == $_POST['csrfToken']) {
    $password = $Security->Salt($Security->secParam($_POST['password']));
    $password = $DB->quoteStr($password);
    $email = $DB->quoteStr($_POST['email']);
    $DB->Query("SELECT * FROM `table` WHERE `email` = $email AND `password` = $password");
    return $DB->GetRow();
}

public function GetRow() {
    return mysql_fetch_array($this->last_query);
}
public function quoteStr($str) {
    return "'".mysql_real_escape_string($str)."'";
}

2 Comments

Answer with no explanatory prose at all. My face ---> :(
I already knew that, i just wanted it put inside an array for easier access although i must prop you for reminding me about securing my "Email" parameter ! Thanks
-1

Marin Sagovac question is the answer.

To break it down a little more, your var_dump output shows that $results is a nested array. The first part of the output:

array(1) { 
[0]=>

shows that $results consists of an array containing 1 element, at index 0, since that's where PHP starts indexing. This is the $results[0] part of Marin's response.

The element 0 of the $results array consists of an array with 8 elements.

[0]=>array(8) { 
[0]=> string(1) "1" ["id"]=> string(1) "1"  
[1]=> string(35) "[email protected]" ["email"]=> string(35) "[email protected]" 

Even though there are only 4 actual results, index 1-4, each one exists twice so that they can either be accessed by index or by its key. Arrays that can be accessed by a unique key, as opposed to an index, are known as associative arrays.

So, in this case, either will return the same value:

echo $results[0]["email"];
echo $results[0][1];

The print_r function would also work, instead of var_dump.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.