oz11 Posted November 12, 2022 Share Posted November 12, 2022 (edited) Been working on this part of my site,... <?php function getLinkTree($pdo, $url) { $res = str_replace(array('https://www.', 'http://www.', 'https://','http://'), '', $url); $stmt = $pdo->prepare("SELECT url FROM links WHERE url LIKE ?"); $stmt->execute(['%$res%']); while ($row = $stmt->fetch()) { echo $row['url']."<br />\n"; } } getLinkTree($pdo, "https://bbc.co.uk"); ?> Instead of it coming back with all the other urls which contain "bbc.co.uk" it doesnt do anything. However in phpmyadmin, when I run the query .. Quote SELECT * FROM `links` WHERE url LIKE '%bbc.co.uk%'; .. it brings back the correct results. What am I doing wrong guys and gals? Edited November 12, 2022 by oz11 Quote Link to comment https://forums.phpfreaks.com/topic/315526-function-not-working-using-like-and-pdo/ Share on other sites More sharing options...
Solution oz11 Posted November 12, 2022 Author Solution Share Posted November 12, 2022 Changed Quote $stmt->execute(['%$res%']); to Quote $stmt->execute(["%$res%"]); and seems to work now. Thanks guys for being here. Quote Link to comment https://forums.phpfreaks.com/topic/315526-function-not-working-using-like-and-pdo/#findComment-1602515 Share on other sites More sharing options...
maxxd Posted November 12, 2022 Share Posted November 12, 2022 PHP won't interpolate variables inside single quotes, so your search string was literally %$str% in the first example. Inside double quotes, however, PHP will replace the variable with the value it contains. Quote Link to comment https://forums.phpfreaks.com/topic/315526-function-not-working-using-like-and-pdo/#findComment-1602547 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.