-2

This one's been bugging me for the past day now. Here's the code:

public function getUserCredits()
{
    $dbl = new databaseManager();
    $dbl->connect_simp();
    $ret = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
    $this->userCredits = $ret['USER_CREDITS'];

    return $this->userCredits;
}

Now when I try to run this code, I get an undefined offset error. Nothing too strange about it when I first saw it, but now it's happening more and more and I can't figure out why.

I can use var_dump(); and var_export(); and it displays the contents of the returned array absolutely fine.

EDIT:

    array(1) {
  [0]=>
  array(50) {
    ["USER_ID"]=>
    string(10) "0000000001"
    [0]=>
    string(10) "0000000001"
    ["USER_USERNAME"]=>
    string(8) "SampleUsername"
    [1]=>
    string(8) "SampleUsername"
    ["USER_PASSWORD"]=>
    string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
    [2]=>
    string(32) "5f4dcc3b5aa765d61d8327deb882cf99"
    ["USER_EMAIL"]=>
    string(0) ""
    [3]=>
    string(0) ""
    ["USER_LEGION"]=>
    string(1) "1"
    [4]=>
    string(1) "1"
    ["USER_ENERGY"]=>
    string(4) "2812"
    [5]=>
    string(4) "2812"
    ["USER_MAX_ENERGY"]=>
    string(4) "2812"
    [6]=>
    string(4) "2812"
    ["USER_SHIELD"]=>
    string(2) "20"
    [7]=>
    string(2) "20"
    ["USER_MAX_SHIELD"]=>
    string(2) "20"
    [8]=>
    string(2) "20"
    ["USER_HULL"]=>
    string(2) "60"
    [9]=>
    string(2) "60"
    ["USER_MAX_HULL"]=>
    string(2) "60"
    [10]=>
    string(2) "60"
    ["USER_CREDITS"]=>
    string(19) "9223372036854775807"
jhmckimm
  • 870
  • 6
  • 15
  • **You are leaving yourself wide open to SQL injection attacks.** Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. http://bobby-tables.com/php has examples to get you started, and [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. – Andy Lester Sep 07 '13 at 19:46
  • Can you add the output of `var_dump()` to the question? – raina77ow Sep 07 '13 at 19:47
  • please add var_dump result too. – Vahid Hallaji Sep 07 '13 at 19:47
  • This uses PDO within a custom class. I really do appreciate the advice, but this isn't what was asked. :| – jhmckimm Sep 07 '13 at 19:48
  • Okay, added in the var_dump(); output. I've cut it short. – jhmckimm Sep 07 '13 at 19:52
  • Going through PDO doesn't matter if you're building SQL statements with outside data. – Andy Lester Sep 07 '13 at 19:54
  • @AndyLester It's all handled and is injection proof. Trust me. :) – jhmckimm Sep 07 '13 at 19:55

4 Answers4

0

You gotta fetch a row first (I assume you extend mysqli).

$result = $dbl->queryDB("SELECT * FROM users WHERE `USER_ID` = ".$this->userId);
$row = $dbl->fetch_assoc($result) ;

$this->userCredits = $row['USER_CREDITS'];
sybear
  • 7,837
  • 1
  • 22
  • 38
0

try

$ret[0]['USER_CREDITS']

instead of

$ret['USER_CREDITS']
Baraa Al-Tabbaa
  • 753
  • 5
  • 11
  • That worked. I kinda feel stupid for not seeing it before. Thank you so much! Will mark as answer when it lets me. In the mean time, would you mind explaining why this works? – jhmckimm Sep 07 '13 at 19:56
  • Ohh, I see it now. It's an array within an array. – jhmckimm Sep 07 '13 at 20:00
  • don't worry, it happens sometimes. this is because the result of sql query is an array of arrays. the first array is all the columns in table the matched the condition, and the second array is the column's fields. – Baraa Al-Tabbaa Sep 07 '13 at 20:01
  • @JordanHughMcKimm You've got an error if user id not found in this case. – Vahid Hallaji Sep 07 '13 at 20:05
  • 1
    @AbuOmar This database class was written by a friend, and it's based on PDO. I'm used to looping through the result set with `mysql_fetch_array();` (hence why I wasn't expecting an array within an array). All this PDO/OOP stuff is quite new to me. Thanks again. :) I better get back to finishing it off now. :P – jhmckimm Sep 07 '13 at 20:15
0

AS your var_dump($ret), Try this to assign user credit otherwise if it is not set, assign 0 value.

if(isset($ret[0]['USER_CREDITS'])){
    $this->userCredits = $ret[0]['USER_CREDITS'];
}
else{
    $this->userCredits = 0;
}
Vahid Hallaji
  • 7,159
  • 5
  • 42
  • 51
0

At first you should watch the result record. As you are getting the details in an array of your database record fetched and it is in array format with numeric keys. Use what @AbuOmar said.

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34