如何使用sql检查两个字段是否具有相同的值?

时间:2014-03-24 13:07:50

标签: php mysql

我在使用mysql语句时遇到问题,我想检查粉丝是否跟随,粉丝跟随用户和用户名是否具有相同的值,这是我的代码:

<?
$selectfan = mysql_query("SELECT * FROM amityfans");
$fanrow = mysql_fetch_assoc($selectfan);
$fan_following = $fanrow['fan_following'];
$fan_followed = $fanrow['fan_followed'];
if ($fan_following == "$user" && $fan_followed=="$username") {
 $addasfan = '<input type="submit" class="button" name="removefriend" value="Remove fan">';
}
else
{
 $addasfan = '<input type="submit" class="button" name="addfriend" value="Add Me as Fan">';
}
echo $addasfan;
?>

但即使'user'和'username'具有相同的值,它也不显示删除扇形按钮。 FYI $ user是登录用户的用户名,$ username是我们看到的个人资料的用户名。

编辑:

我在删除具有用户和用户名的粉丝的特定行时遇到问题这里是我的代码:

<?php
//$user = logged in user
//$username = user who owns profile
if (isset($_POST['removefriend'])) {
$removefan = mysql_query("DELETE FROM amityusers WHERE fan_following='$user' && fan_followed='$username'");
}
?>

1 个答案:

答案 0 :(得分:0)

fan_following列中的 FRED fan_followed中的 BOB 生成了Add Me as Fan按钮。

fan_following列中的 BOB fan_followed中的 BOB 生成了Remove fan按钮。

通过更改:

if ($fan_following == "$user" && $fan_followed=="$username")

为:

if ($fan_following == $fan_followed)

^ - «(我相信也应该注意你的删除问题,我也在我在同一个脚本中设置的函数中使用你的代码成功测试过。

这是我通过设置一个快速数据库来完成的。

使用以下内容:

<?php

$DB_HOST = "xxx";
$DB_NAME = "xxx";
$DB_PASS = "xxx";
$DB_USER = "xxx";


$db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($db->connect_errno > 0) {
  die('Connection failed [' . $db->connect_error . ']');
}

$user = "FRED";
$username = "BOB";

 $selectfan = mysqli_query($db,"SELECT * FROM amityfans");

$fanrow = mysqli_fetch_assoc($selectfan);
$fan_following = $fanrow['fan_following'];
$fan_followed = $fanrow['fan_followed'];

 if ($fan_following == $fan_followed) {

 $addasfan = '<input type="submit" class="button" name="removefriend" value="Remove fan">';

}
else
{
 $addasfan = '<input type="submit" class="button" name="addfriend" value="Add Me as Fan">';
}
echo $addasfan;
?>