2014-11-17 3 views
1

나는 잠시 동안 지금과 싸우고있다. 나는 단순히 단일 값을 반환 할 수 없다. 배열을 잘 반환 할 수 있습니다. 하지만 단일 값은 아닙니다. 난 그냥 세 개의 열이있는 데이터베이스를 가지고, 컬럼은 사용자 ID이며, 2 열은 friendID이며 3 열은 이제 다음과 같은 기능을 할 경우는 값 0 또는 1MySQL에서 PHP로 함수의 단일 값을 반환하는 방법은 무엇입니까?

을 가지고 IfFriends입니다 :

function CheckFriend($id, $friendid){ 
global $mysqli; 

$stmt = $mysqli->prepare("SELECT IfFriends FROM friends WHERE userID=? 
          AND friendID=?"); 
$stmt->bind_param("ii", $id, $friendid); 
$stmt->bind_result($IfFriends); 
$stmt->execute(); 

    return $IfFriends; 

이제 생각을 내가 잘못 했니? 나는 SQL 쿼리를 시도하고 그것이 그러나 1. 날 다시 제공 내가 기능을 사용하는 경우는 PHP에서 0

내가 그것을 이렇게 기록 된이 날 다시 제공, 작동 :

$ iffriends = CheckFriend ($ _ SESSION [ "id"], $ _REQUEST [ "friendid"]); 내가 할 때

는 그리고 :

그것은 항상 상관없이, 0을 나에게 다시 제공하지 않습니다 $ iffriends

에코.

누군가가 시간을내어 내가 뭘 잘못하고 있는지 말해 줄 수 있기를 바랍니다. 미리 감사드립니다.

답변

2

읽기 the documentation :

참고 : 모든 열 전에 mysqli_stmt_fetch를 호출하기 mysqli_stmt_execute()와 후 바인드되어야

주(). 연결된 열 유형에 따라 변수가 자동으로 해당 PHP 유형으로 변경 될 수 있습니다.

그래서 bind로 호출은 execute 후이어야하고 첫 번째 행을 읽어 fetch를 호출해야합니다.

// Parameters bound before execute 
$stmt->bind_param("ii", $id, $friendid); 

// Execute the statement 
$stmt->execute(); 

// Result location bound (but this doesn't retrieve anything) 
$stmt->bind_result($IfFriends); 

// Fetch the first row of the result, stored into the result variable bound above 
if($stmt->fetch()) 
{ 
    // Make sure you check the result of fetch(). 
    // If it returned false, no rows were returned. 
    // In this case, it returned true, so your value is in $IfFriends 
} 

// If you happened to have multiple rows, you would call fetch again 
// to retrieve the next row. 
if($stmt->fetch()) 
{ 
    // You can continue calling fetch() until it returns false. 
    // Most often this is done in a loop like: 
    // while($stmt->fetch()) { /* Do something with the row */ } 
} 
+0

나는 실제로 그것을 여러 번 읽었습니다. 그것을 이해하는 데 어려움이 있습니다. 그러나 나는 적어도 그것을 알아낼 것입니다. 나는 그 대답이 거기에 있음을 알고 있습니다. – Martin

+0

@ Martin, 코드 예제를 추가했습니다. –

+0

고맙습니다. 전에 배열을 계속 사용했습니다. – Martin

0

값을 검색 할 때이 기능을 사용할 수 있습니다. 이것은 PHP 함수입니다. 단일 반환 값의 쿼리가있는 쿼리로 호출하면됩니다.

function value_return($query){ 

$result= mysql_query($query); 
$value=mysql_result($result,0); 
return $value; 

} 
관련 문제