2016-04-03 19 views
0

device_token alanını aşağıdaki php deyiminde $token adlı bir değişkene nasıl ayarlayabilirim? W3Schools SQL belgeleri kapalı göreDeğeri ayarlama

result = query("SELECT device_token, IdPhoto, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 1") ; 

: The WHERE clause is used to extract only those records that fulfill a specified criterion. Bu nasıl yukarıdaki kodda kullanılabilir? Belirli bir jetonu almak için

http://www.w3schools.com/sql/sql_where.asp

+1

içeri ayarlamak istediğiniz değer 50 kayıtları döndürür Kişisel seçme sorgusu '$ token' –

+0

ben $ belirteçteki device_token değeri saklamak istiyor . – user3233623

cevap

1

Bir WHERE maddesini ve belirli userid gerekir.

$id = 1; //You may get this from a form/session you created for login user  
$dbh = new PDO('mysql:host=localhost;dbname=dbname', 'root', 'password');  
$result = $dbh->query("SELECT device_token, IdPhoto, IdUser 
         FROM photos WHERE IdUser='$id' 
         ORDER BY IdPhoto DESC LIMIT 1"); 
$devicetoken = $result->fetch(PDO::FETCH_ASSOC); 

echo $devicetoken['device_token']; 

Ancak, belirli bir belirteç gerekir ve tüm belirteçleri dönmek istemiyorsanız, sen WHERE maddesini ve LIMIT gerekmez. Ancak, LIMIT, ifadenin geri döneceği satır sayısını değiştirme seçenekleri sunar.

$result = $dbh->query("SELECT device_token, IdPhoto, IdUser 
         FROM photos 
         ORDER BY IdPhoto DESC"); 
$token = $result->fetch(PDO::FETCH_ASSOC);//store the device token here in $token 

Şimdi cihaztoken, tüm device_token dizisini içerir. Sen foreach döngüsü ve ve kullanma bunların her birini erişebilirsiniz ise:

foreach($token as $key=>$val){ 
     echo $val.'<br>'; 
}