Php 如果mysql结果不存在,则返回?
嗨,我的网站上有一个基本的喜欢系统。基本上,一旦用户单击like,就会将user_id-has_like从0设置为1 如果他们的用户\u id\u has\u like设置为0,则会显示like链接,如果设置为1,则会显示inspect链接。然而,我想添加另一个条件,即如果结果不在mysql中,则回显类似的链接 有人能告诉我在哪里,我会添加什么来实现这一点,请Php 如果mysql结果不存在,则返回?,php,mysql,Php,Mysql,嗨,我的网站上有一个基本的喜欢系统。基本上,一旦用户单击like,就会将user_id-has_like从0设置为1 如果他们的用户\u id\u has\u like设置为0,则会显示like链接,如果设置为1,则会显示inspect链接。然而,我想添加另一个条件,即如果结果不在mysql中,则回显类似的链接 有人能告诉我在哪里,我会添加什么来实现这一点,请 <div class="profile_likes"> <?php $user_like_set = user_lik
<div class="profile_likes">
<?php
$user_like_set = user_like_status();
while ($like = mysql_fetch_array($user_like_set))
if ($like['user_id_has_liked'] == '0') { ?>
<a href="like_profile.php?to=<?php echo "$profile_id"; ?>">Like</a> | <?
$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {
echo "". $likes['likes'] ." People Like ".$profile[2]."";
//$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) {
?>
<? } }?>
<?php
$user_like_set = user_like_status();
while ($like = mysql_fetch_array($user_like_set))
if ($like['user_id_has_liked'] == '1') { ?>
<a href="unlike_profile.php?to=<?php echo "$profile_id"; ?>">Unlike</a> | <?
$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {
echo "". $likes['likes'] ." People Like ".$profile[2]."";
//$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) {
?>
<? } }?>
</div>
|
|
尝试在使用echo/print函数时保持一致性,并通过终止php标记来模拟它们,代码将变得更加清晰
<?php
function printLikeLink($likeLink){
echo "<a href='{$likeLink}'>Like this profile</a>";
}
$user_like_set = user_like_status();
if ($like = mysql_fetch_array($user_like_set)){
if ($like['user_id_has_liked'] == '1') {
echo "<a href=\"unlike_profile.php?to={$profile_id}\">Unlike</a>|";
$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {
echo "". $likes['likes'] ." People Like ".$profile[2]."";
//$check_new_duos_set = check_new_escort_duos(); while ($newd = mysql_fetch_array($check_new_duos_set)) {
}
}else printLikeLink("like_profile.php?to={$profile_id}");
}else printLikeLink("like_profile.php?to={$profile_id}"); // Edit this link.
?>
你应该试试看。它将提供记录集中存在的行数。
除此之外,请注意,您正在使用的API已被弃用。在mysql网站上寻找建议的替代方案。试试这个,我刚刚做了一个更改,您的用户喜欢它的状态();应该返回true还是false
<div class="profile_likes">
<?php
$user_like_set = user_like_status();//should return true or false
if ($user_like_set == false){
$what = "Like";
}else{
$what = "Unlike";
}?>
<a href="like_profile.php?to=<?php echo "$profile_id"; ?>"><?php echo $what ;?></a> |
<?php
$count_likes_set = count_likes();
while ($likes = mysql_fetch_array($count_likes_set)) {
echo "". $likes['likes'] ." People Like ".$profile[2]."";
}
?>
</div>
|
这不起作用,因为如果未找到任何记录,mysql\u fetch\u数组将返回false,因此它不会进入循环。非常正确,谢谢。如果他只是按照代码的建议寻找一个条目,他可以用If语句替换while,并添加另一个else子句。那么有人能告诉我需要做什么吗,我很困惑。谢谢