3

I'm new in the PHP world and I need a bit of help here. I'm trying to extract a data from database, I'm using PDO to do it. I have the following PHP code without success, throwing back error notice:

$pairingsistem='1'; 
$pecahan='1';

if($pairingsistem == "1"){

$skrg=time();
$tablaz = $pdo->query("SELECT * FROM tb_gh where saldo > 0 and status='pending' order by id ASC limit 0,1");
while ($registroz = $tablaz ->fetchAll(PDO::FETCH_ASSOC)){ 
//use $results   
$kurirz=$registroz["username"]; //line 47 starts here
$biayaz=$registroz["saldo"];
$idnyaz=$registroz["id"];
$bankeem=$registroz["bank"];
$norekeem=$registroz["norek"];
$bitcoineem=$registroz["bitcoin"];
$pmeem=$registroz["perfectmoney"];
$fasapayeem=$registroz["fasapay"];
$namaeem=$registroz["nama"];
$phoneeem=$registroz["phone"];
$emaileem=$registroz["email"];
$paketzeem=$biayaz*$pecahan;
$surabaya=$paketzeem/$pecahan;
//shortline

Notice: Undefined index: username in /home/u427750052/public_html/automatch.inc.php on line 47

Notice: Undefined index: saldo in /home/u427750052/public_html/automatch.inc.php on line 48

Notice: Undefined index: id in /home/u427750052/public_html/automatch.inc.php on line 49

Notice: Undefined index: bank in /home/u427750052/public_html/automatch.inc.php on line 50

Notice: Undefined index: norek in /home/u427750052/public_html/automatch.inc.php on line 51

Notice: Undefined index: bitcoin in /home/u427750052/public_html/automatch.inc.php on line 52

Notice: Undefined index: perfectmoney in /home/u427750052/public_html/automatch.inc.php on line 53

Notice: Undefined index: fasapay in /home/u427750052/public_html/automatch.inc.php on line 54

Notice: Undefined index: nama in /home/u427750052/public_html/automatch.inc.php on line 55

Notice: Undefined index: phone in /home/u427750052/public_html/automatch.inc.php on line 56

Notice: Undefined index: email in /home/u427750052/public_html/automatch.inc.php on line 57

this has been the warnings. Though I have troubleshot all I could within the scope of my knowledge on this so far.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
MK Smith
  • 111
  • 10
  • Thanks chris. i think it worked. So if i am using fetchall, i just while '$registroz = $tablaz ->fetchAll(PDO::FETCH_ASSOC);' – MK Smith Jul 31 '17 at 00:48
  • 1
    *"Don't loop with fetchAll, loop with fetch, or fetchall"* - @chris85 You said not to use `fetchAll` but said "or fetchall"`, typo? It's a bit confusing lol – Funk Forty Niner Jul 31 '17 at 00:50
  • Thanks to you'all. i think the problem has been resolved. :-) – MK Smith Jul 31 '17 at 00:52
  • 1
    @chris85 I suggest you place an (detailed) answer for this, since the undefined index usually used to dupe questions of this nature with the errors shows, doesn't cover this. There is a db-related answer that was added recently but it doesn't cover this particular issue. – Funk Forty Niner Jul 31 '17 at 00:53
  • if Chris posts an answer, consider accepting it in order to mark the question as solved and welcome to Stack Overflow. Edit: and he has. – Funk Forty Niner Jul 31 '17 at 00:56
  • @chris85 You're welcome. Looks good to me. – Funk Forty Niner Jul 31 '17 at 00:58
  • 6
    To anyone wanting to mark this question as a duplicate of the usually-used `undefined index` Q&A, this is not a duplicate of it, it's a particular case. – Funk Forty Niner Jul 31 '17 at 01:01
  • @LegiSmith Please don't forget to accept the answer. – chris85 Jul 31 '17 at 16:13

1 Answers1

3

Your while and fetchAll are throwing you off here. You either need to loop a fetch or fetchall then iterate over the returned result.

So either:

while ($registroz = $tablaz ->fetch(PDO::FETCH_ASSOC)){ 

or

$registroz = $tablaz ->fetchAll(PDO::FETCH_ASSOC);
foreach($registroz as $row) {

but since you have it returning only 1 row you don't need a loop or fetchall.

$registroz = $tablaz ->fetch(PDO::FETCH_ASSOC);

should do the trick.

chris85
  • 23,846
  • 7
  • 34
  • 51
  • It's pretty interesting to see the undefined index notices show when using PDO like that. I hardly use PDO myself (oh I stand at getting criticized by someone here hahaha!!). This is a first for me. – Funk Forty Niner Jul 31 '17 at 00:57
  • Thanks. this was really the best way to explain in my terms. :-) – MK Smith Jul 31 '17 at 00:59
  • 2
    @Fred-ii- `$registroz` with the fetchall is a multidimensional array, the names are at the second level. I think `$kurirz=$registroz[0]["username"];` would have worked also. – chris85 Jul 31 '17 at 00:59
  • 1
    @chris85 Yep. Btw, can you upvote [this comment](https://stackoverflow.com/questions/45406109/having-trouble-with-pdo-notice-undefined-index#comment77772907_45406109) please? – Funk Forty Niner Jul 31 '17 at 01:14