Reputation: 641
Not sure how I am getting the undefined index error when my SELECT already gets the column that I want.
I have three tables:
clinics_branch.add.php
<?php
session_start();
require_once('clinics_branch_list.vc.php');
?>
<?php echo $lstBranch['usrmerchant']; ?>
the undefined index is in the echo usrmerchant
clinics_branch_list.vc.php
require_once($routePath . "_mc/Merchant.mc.php");
$mcMerchant = new Merchant_MC();
$lstBranch = $mcMerchant->SelectMainBranch_ByMerchantBranch($db, $merchantid);
Merchant.mc.php
Class Merchant_MC {
public function SelectMainBranch_ByMerchantBranch($db, $merchantid) {
$stmt = $db->prepare(
" SELECT mb.merchantid, m.usrmerchant
FROM `merchant_branch` mb
LEFT JOIN `merchant` m ON mb.merchantid = m.merchantid
WHERE mb.merchantid = $merchantid "
);
$stmt->bindValue(':merchantid', $merchantid, PDO::PARAM_INT);
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $row;
}
}
here's the output of my select, as you can see there is content so the SQL is working
any help is appreciated, the full error is:
Notice: Undefined index: usrmerchant in clinics_branch_add.php
UPDATE:
changing my SQL line to WHERE mb.merchantid = :merchantid
still shows the same error.
UPDATE:
switching to innjer join does not work either
SELECT mb.merchantid, m.usrmerchant FROM merchant_branch mb INNER JOIN merchant m ON mb.merchantid = m.merchantid WHERE mb.merchantid = :merchantid
Upvotes: 0
Views: 44
Reputation: 147166
You're doing a fetchAll
, so $lstBranch
will actually be an array of rows looking something like this:
array(0 => array('merchantid' => 30, 'usrmerchant' => 'dermadent'))
and you will need to access the usrmerchant
value as
$lstBranch[0]['usrmerchant'];
Upvotes: 2
Reputation: 731
Change
SELECT mb.merchantid, m.usrmerchant
FROM `merchant_branch` mb
LEFT JOIN `merchant` m ON mb.merchantid = m.merchantid
WHERE mb.merchantid = $merchantid "
To
SELECT mb.merchantid, m.usrmerchant
FROM merchant_branch mb
INNER JOIN marchant m
ON mb.merchantid = m.merchantid
WHERE mb.merchantid = :merchantid
Upvotes: 0