Use of named parameters in MySQL subquery with PHP8 [closed]

4 weeks ago 31
ARTICLE AD BOX

I have 2 tables customers and addresses (each customer can have many addresses) The customers table has two fields of interest: userName (varchar) userID (varchar) The addresses table has two fields of interest userID (varchar) addressName (varchar) I want to select the addressNames for a particular customer using:

$customerName = 'mmcp42'; $sql = "SELECT addressName FROM addresses WHERE userID = (SELECT userID FROM customers WHERE userName = :userName)"; $stmt = $db->prepare($sql); $stmt->bindParam(':userName', $customerName); $stmt->execute(); $addresses = $stmt->fetchall();

This works fine with PHP 7.4, but fails with PHP 8.0 and above; it returns an empty array. The only way I can get it to work is to run the sub-query first to get the userID then plug it in as a parameter to the main query, which is klunky but has the merit that it works with PHP 7.4 and PHP 8. I cannot understand what I am doing wrong. Can you please show me the error?

I edited it to show a sample value of $customerName

Read Entire Article