我有一个查询可以提取用户收藏夹列表并在页面上显示图像。我希望能够做的是,如果没有要显示的收藏夹来显示文件目录中的模板图像。
我正尝试这样做,但出现了这个错误:
Warning: file_exists() expects parameter 1 to be string, resource given in /Applications/XAMPP/xamppfiles/htdocs/PTB1/includes/mod_favourites/favourites.php on line 28
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in /Applications/XAMPP/xamppfiles/htdocs/PTB1/includes/mod_favourites/favourites.php on line 33
我下面的代码有人可以帮助我并告诉我如何做我想做的事吗?
谢谢。
<?php
$favorites_set_more = get_user_favorites_more();
if (!file_exists($favorites_set_more)) {
$favorites_set_more = "data/photos/0/_default.jpg";
}
while ($favorites2 = mysql_fetch_array($favorites_set_more)) {
echo "<a href=\"profile.php?id={$favorites2['favorite_id']}\"><img width=\"90px\" height=\"90px\" class=\"favorites_pic2\" src=\"data/photos/{$favorites2['favorite_id']}/_default.jpg\" /></a>";
?>
<? } ?>
这是 favourites_set_more 函数:
function get_user_favorites_more() {
global $connection;
global $_SESSION;
$query = "SELECT f.favorite_id, p.display_name
FROM ptb_favorites f, ptb_profiles p
WHERE f.user_id =".$_SESSION['user_id']."
AND p.user_id = f.user_id
LIMIT 4,12";
$favorites_set_more = mysql_query($query, $connection);
confirm_query($favorites_set_more);
return $favorites_set_more;
}
最佳答案
如果您阅读了错误消息,它表示 file_exists()
需要一个 string
但您提供的是一个 resource
。所以你应该看看 get_user_favorites_more()
返回了什么。显然它不是一个字符串
。
确保您的 get_user_favorites_more()
函数在没有设置收藏夹的情况下返回一个不存在的图像链接,这样 file_exists
函数将返回 false 和您的默认值将显示图像。
当您查看 get_user_favorites_more()
函数时,您将返回 mysql_query($query, $connection)
的结果。这是一个资源
,而不是一个字符串
。所以你不能做 file_exists($favorites_set_more)
。您可以将其更改为:
<?php
//Get the favorites for the user
$favorites_set_more = get_user_favorites_more();
if (mysql_num_rows($favorites_set_more) == 0) {
echo "<img width=\"90px\" height=\"90px\" class=\"favorites_pic2\" src=\"data/photos/0/_default.jpg\" />";
}
else {
while ($favoriteImg = mysql_fetch_assoc($favorites_set_more)) {
echo "<a href=\"profile.php?id=" . $favoriteImg['favorite_id'] . "\">";
echo "<img width=\"90px\" height=\"90px\" class=\"favorites_pic2\" src=\"data/photos/" . $favoriteImg['favorite_id'] . "/_default.jpg\" />";
echo "</a>";
}
}
?>
关于php - 如果查询不存在,显示模板图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14476366/