PDO throws notification: Undefined index when using fetchAll in WHILE loop
I'm new to the PHP world and need a little help here. I am trying to fetch data from a database, I am using PDO for this. I have the following PHP code without success, dropping the error notification:
$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
Note: Undefined index: username at / home / u 427750052 / public_html / automatch.inc.php on line 47
Note: Undefined index: saldo in / home / u 427750052 / public_html / automatch.inc.php on line 48
Note: Undefined index: id in / home / u 427750052 / public_html / automatch.inc.php on line 49
Note: Undefined index: bank in / home / u 427750052 / public_html / automatch.inc.php on line 50
Note: Undefined index: norek at / home / u 427750052 / public_html / automatch.inc.php on line 51
Note: Undefined index: bitcoin at / home / u 427750052 / public_html / automatch.inc.php on line 52
Note: Undefined index: perfectmoney at / home / u 427750052 / public_html / automatch.inc.php on line 53
Note: Undefined index: fasapay in / home / u 427750052 / public_html / automatch.inc.php on line 54
Note: Undefined index: nama in / home / u 427750052 / public_html / automatch.inc.php on line 55
Note: Undefined index: phone at / home / u 427750052 / public_html / automatch.inc.php on line 56
Note: Undefined index: email at / home / u 427750052 / public_html / automatch.inc.php on line 57
These were the warnings. Although I have problems that I could within my knowledge of this so far.
source to share
Yours while
and fetchAll
throw you back here. You need to either loop fetch
or fetchAll
and then iterate over the result you get.
So either:
while ($registroz = $tablaz ->fetch(PDO::FETCH_ASSOC)){
or
$registroz = $tablaz ->fetchAll(PDO::FETCH_ASSOC);
foreach($registroz as $row) {
but since you are only returning one row, you don't need a loop or fetchAll
.
$registroz = $tablaz ->fetch(PDO::FETCH_ASSOC);
must do the trick.
source to share