apatik
apatik

Reputation: 383

SQL request not returning anything when replacing a string by a variable

I'm trying to setup a simple function to update the path of subfolders when the main folder is renamed.

Here is my code :

case 'rename':

$dirpath = rtrim(preg_replace('~/+~', '/', (str_replace('\\', '/', $_POST['old-dirpath']))), '/');
$new_dirpath = rtrim(preg_replace('~/+~', '/', (str_replace('\\', '/', $_POST['new-dirpath']))), '/');

$sql = "
    UPDATE privatedir SET
    dirpath = '{$new_dirpath}'
    WHERE dirpath = '{$dirpath}'
    ";

    $db->execute($sql);


$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%.$dirpath.%'");

$folders = $db->fetchAll($rs);
print_r ($folders); print_r($dirpath); die;
foreach ($folders as $folder){
    echo $folder['dirpath']; die;
    $folder_new_dirpath = str_replace($dirpath, $new_dirpath, $folder['dirpath']);

    $sql = "
        UPDATE privatedir SET
        dirpath = '{$new_dirpath}'
        WHERE dirpath = '{$folder['dirpath']}'
        ";

        $db->execute($sql);
}

My problem concerns the line $rs = $db->query("SELECT * FROM privatedir WHEREdirpathLIKE '%.$dirpath.%'"); even though there are matching results in my database it won't return me anything, if I print_r $folders it just prints me an empty array.

However if I replace .$dirpath. by the string directly (for example C:/wamp/www/gg/ftp/repository/folder1 ) it will return me the matching results and print_r $folders will print me the corresponding arrays.

What am I doing wrong ?

Upvotes: 0

Views: 37

Answers (2)

Castro Alhdo
Castro Alhdo

Reputation: 291

Put variable in quote like thi

    $rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%'".$dirpath."'%'");

Upvotes: 2

Phiter
Phiter

Reputation: 14992

Thing is that you don't need the dots around, because you're not concatenating the string.

This should work:

$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%$dirpath%'");

You can also do

$rs = $db->query("SELECT * FROM privatedir WHERE `dirpath` LIKE '%".$dirpath."%'");

if you like it better.

Upvotes: 1

Related Questions