PHP unlink正在请求一个字符串,试图给它sql字符串


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

我试图从某个文件夹中删除一个项目,我需要在项目的路径中传递unlink()。由于某种原因,它说我没有返回一个字符串,我不知道该怎么做。

下面是所有的代码:

$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返回语句HANDLE,而不是字符串。您需要从结果中获取一行:

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

mysql_query返回一个资源,而不是字符串。

使用:

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

过程:

  1. 查询数据库,将资源句柄存储在变量($query)中。
  2. 取行(如果只有一行)保存在$result
  3. 获取path列的值
  4. 使用该值删除文件

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

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

mysql_query实际上并没有返回查询所选择的值,而是返回一个结果资源。您可以使用mysql_result(,,)函数来获取所需的信息。