PHP를 가져 오지 않았다json_decode의를 받게되며 PDO fetchAll 후 PDO는
public function addFriend($user)
{
$sth = $this->db->prepare('SELECT user_id FROM users WHERE user_name = ? ');
$sth->execute(array($user->friend));
//Works but then everything else not work
//return json_encode($sth->fetch());//NOT work
return json_encode($sth->fetchAll());
$count = $sth->rowCount();
if ($count == 1) {
// fetch one row (we only have one result)
$result = $sth->fetchAll();//NOT WORK
//$result = $sth->fetch();//NOT WORK
$ifexist = $this->db->prepare("SELECT friend_id FROM friends WHERE user_id = ? AND friend_id = ?");
$ifexist->execute(array($user->id, $result->user_id));
$count2 = $ifexist->rowCount();
if ($count2 == 1) {
//Friend already exist in friendslist
return false;
}else{
$token = $this->getToken(50);
$uid = $user->id;
$fid = $result->user_id;
$sth = $this->db->prepare('INSERT INTO friends
(user_id, friend_id, token)
VALUES (:user_id, :friend_id, :token)');
//we loop 2 times over the bindparams
//1st time with above settings ($uid & $fid) and 1 time with settings beneath
for($i = 1; $i <= 2; $i++) {
$sth->bindParam(':user_id', $uid);
$sth->bindParam(':friend_id', $fid);
$sth->bindParam(':token', $token);
$sth->execute();
//token stay the same
$token = $token;
//switch clients id($uid) as friends id
$uid = $result->user_id;
//switch friends id ($fid) as clients id
$fid = $user->id;
}
}
}
}
jQuery를이 : MVC보기에 내가 jQuery로 바닥 글에서 데이터를 추가 : 나는 MVC 모델에서이
function addFriend() {
$('#indicator').show();
//var checkFriendsName = $('#friendsName').val();
//alert(checkFriendsName);
var User = new Object();
User.id = uiD;
User.friend = $('#friendsName').val();
var userJson = JSON.stringify(User);
$.post(BaseUrl+"chat/run",
{
action: 'insert_friend',
user: userJson
},
function(data, textStatus) {
renderaddFriend(data);
$('#indicator').hide();
},
'json'
)
}
function renderaddFriend(jsonData) {
var checkInputName = $('#friendsName').val();
if (jsonData.length == 0) {alert(checkInputName+' not found') }//not work
$.each(jsonData, function(index, user){
alert(user.user_id);
});
}
어떻게 할 수 나는 jquery renderaddFriend 함수에 여전히 json_encode return을 전달합니까?
당신은이'하고있는 (-)>가 가져 오는''다음 -> fetchAll()'. 후자는 결과 행이 하나 뿐인 경우 빈 배열을 반환합니다. –
나는 다시 시도해 보았다. 나는 같은 사실을 이야기했기 때문에 이미 시도한 것 같다.하지만 잠시 후에 다시 작동하지 않았다. – HenryW
아마도'return json_encode ($ result)'를하고 싶습니까? –