Jump to content

Function not working (using LIKE and PDO)


Go to solution Solved by oz11,

Recommended Posts

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 by oz11
  • oz11 changed the title to Function not working (using LIKE and PDO)

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.