Search code examples
phpmysqlpathsql-deleteunlink

PHP unlink is asking for a string trying to give it sql string


I am trying to delete an item from a folder somewhere, and I need to pass in the path of the item to unlink(). For some reason it says I am not returning a string, which I am not sure how to do.

Here is all the code:

$filePath = "Select path FROM library where id=" .$id;

    unlink(mysql_query($filePath));

    $library =  "DELETE FROM library WHERE id =" .$id;
    $files =    "DELETE FROM files WHERE library_id=" .$id;
    mysql_query($library) or die('Unable to update using sql '.$library.' '.mysql_error($link));
    mysql_query($files) or die('Unable to update using sql '.$files.' '.mysql_error($link));

Solution

  • mysql_query returns a statement HANDLE, not a string. You need to fetch a row from the result:

    $res = mysql_query($filePath) or die(mysql_error());
    $row = mysql_fetch_assoc($res);
    unlink($row['path']);