2016-04-07 94 views
2

這是在我的api中使用的語句。下面是整個函數中的第二個變量$result。如何改變它使用PDO?將功能更改爲PDO

$result = query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ; 

整體功能

function stream($IdPhoto=0) { 

if ($IdPhoto==0) { 

    //Load Photos  
    $result = query("SELECT IdPhoto, device_token, /*title,*/ IdUser FROM photos /*p*/ ORDER BY IdPhoto DESC LIMIT 300 "); 

} else { 

    //Want this to be PDO :) 
    $result = query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ; 

} 

將在第二個結果變量的地方此代碼的工作?我的目標是將其更改爲PDO,以便更成功地進行擴展。到目前爲止,還沒有輸出。

$dbh = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'A very secure password!!!');  
    $result = $dbh->query("SELECT p.IdPhoto, p.device_token, /*title,*/ p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto='%d'", $IdPhoto) ; 

這是我的數據庫連接值

'host'  => 'localhost', 
'dbname' => 'pushchat', 
'username' => 'pushchat', 
'password' => 'A very secure password!!!', 

更新 PHP日誌文件

[08-Apr-2016 03:39:25 Europe/Berlin] PHP Notice: Undefined index: IdPhoto in /Applications/MAMP/htdocs/Hi2/index.php on line 44 

更新

用於測試文件和TH PDO的語法e瀏覽器返回了一行。我怎樣才能讓應用程序通過j兒子來調用它?

<?php 
$conn = new PDO('mysql:host=localhost;dbname=pushchat', 'pushchat', 'd]682\#%yI1nb3'); 

function new_function() 

{ 
echo "hi<br>"; 

$an_int = 12;  

// If this is an integer 

if (is_int($an_int)) 

{ 

global $conn; 

$stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 "); 

$stmt->execute(); 

$result = $stmt->fetch(PDO::FETCH_ASSOC); 

print_r($result); 

$swag_Bag = 'p.device_token'; 

    print_r($swag_Bag); 

} 

} 
new_function(); 

?> 

更新

這是應用程序如何就叫原stream功能。

-(void)refreshStream 
{ 
//The "stream" command from the web API 
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:@"stream", @"command", nil] onCompletion:^(NSDictionary *json) 
{ 
    //got stream 
    [self showStream:[json objectForKey:@"result"]]; 

    ... 

    }]; 
} 
+5

的PDO文檔是非常好的,它有大量的例子。閱讀它,你應該知道如何使用它。 – Barmar

+2

使用預處理語句是個好習慣,因爲它更好地防止sql注入。 – gommb

+0

@ user5200287感謝您的輸入。要使用PDO,我必須更改整個函數,還是隻需要我需要的變量? – user3233623

回答

3

試試這個:

$conn = new PDO('mysql:host=localhost;dbname=dbname', 'root', 'password'); 
function stream($IdPhoto=0) { 
global $conn; 

if ($IdPhoto==0) { 

    // load the last 300 photos 
    $stmt = $conn->prepare("SELECT IdPhoto, device_token, IdUser FROM photos ORDER BY IdPhoto DESC LIMIT 300 "); 
    $stmt->execute(); 
    $result = $stmt->fetch(PDO::FETCH_ASSOC); 

} else { 

//This is the statement i need to call explicitly via PDO 

    //do the same as above, but just for the photo with the given id 
    $stmt = $conn->prepare("SELECT p.IdPhoto, p.device_token, p.IdUser FROM photos p JOIN login l ON (l.IdUser = p.IdUser) WHERE p.IdPhoto= :idphoto "); 
    $stmt->execute([':idphoto' => $IdPhoto]); 
    $result = $stmt->fetch(PDO::FETCH_ASSOC); 

} 

if (!$stmt->errorInfo()[0]) { 
    // if no error occured, print out the JSON data of the 
    // fetched photo data 
    print json_encode($result); 
} else { 
    //there was an error, print out to the iPhone app 
    errorJson('Photo stream is broken'); 
} 
} 
+2

將其轉換爲適當的預備聲明很不錯。新的數組符號使得這非常簡單。 – tadman

+1

@tadman謝謝。 – gommb

+0

非常感謝代碼@ user5200287我肯定會使用未來的語法。現在它不會爲我輸出嗎?這可能是因爲我擁有的文件中的其他方法,它需要放在恰當的位置嗎?我使用這行代碼連接'$ conn = new PDO('mysql:host = localhost; dbname = pushchat','pushchat','d] 682 \#%yI1nb3');' – user3233623