开发者

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 sur开发者_C百科e 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));


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']);


mysql_query returns a resource, not a string.

Use:

$query = mysql_query($filePath);
$result = mysql_fetch_array($query);
$path = $result['path'];
unlink($path);

The process:

  1. Query the database and store the resource handle in a variable ($query).
  2. Fetch the row (if there is only one) and store the row in $result.
  3. Fetch the value for the path column.
  4. Use that value to delete the file.

See the manual for detailed information and usage notes.


http://php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-result.php

mysql_query does not actually return the values of that your query selected, but a result resource. You can use the function mysql_result(, , ) to get the wanted information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜