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:
- Query the database and store the resource handle in a variable (
$query
). - Fetch the row (if there is only one) and store the row in
$result
. - Fetch the value for the
path
column. - 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.
精彩评论