美好的一天,
我在使用以下php代码时遇到了大量麻烦, 它的伪代码如下:
检查表中是否存在关系 如果它不存在则向表添加关系。 否则得到当前的关系并返回它。
代码如下:
$result = mysql_query("SELECT * FROM friends WHERE (user_id = '" .$me. "' AND user_id2 = '" .$other. "') OR (user_id2 = '" .$me. "' AND user_id = '" .$other. "')");
if($result == false){
mysql_query("INSERT INTO `friends`(`user_id`, `user_id2`, `status`) VALUES ('" .$me. "', '" .$other. "', 'pending')");
echo("Friend Request Sent");
}
else{
$res = mysql_query("SELECT 'status' FROM friends WHERE (user_id = '" .$me. "' AND user_id2 = '" .$other. "')");
$rows = array();
while($r = mysql_fetch_assoc($res)) {
$rows[] = $r;
}
echo json_encode($rows);
}
提前致谢
答案 0 :(得分:1)
你应该尝试以下条件
if(mysql_num_rows($result)){
mysql_query("INSERT INTO `friends`(`user_id`, `user_id2`, `status`) VALUES ('" .$me. "', '" .$other. "', 'pending')");
echo("Friend Request Sent");
}
并且else块应该与第一个查询相同,它应该看起来像
else{
$res = mysql_query("SELECT 'status' FROM friends WHERE (user_id = '" .$me. "' AND user_id2 = '" .$other. "') OR (user_id2 = '" .$me. "' AND user_id = '" .$other. "')") ;
$rows = array();
while($r = mysql_fetch_assoc($res)) {
$rows[] = $r;
}
echo json_encode($rows);
}