2013-03-02 62 views
0

我想把這個查詢轉換爲PDO:請人誰可以幫助將不勝感激如何將更新查詢從mysql轉換爲PDO?

function updateonlinesession(){   
if(isset($_SESSION['username']['id'])){ 
$uid = $_SESSION['username']['id']; 
$page = $_SERVER['REQUEST_URI']; 
$ip = $_SERVER['REMOTE_ADDR']; 
$username = $_SESSION['logged']; 

("UPDATE site_user SET dateupdated = now(),ip = '$ip' WHERE 
username = '".mysql_real_escape_string($_SESSION['username'])."'"); 
} 

} 
This is What I have tried 
<?php 

function updateonlinesession(){   
if(isset($_SESSION['username']['id'])){ 
$uid = $_SESSION['username']['id']; 
$page = $_SERVER['REQUEST_URI']; 
$ip = $_SERVER['REMOTE_ADDR']; 
$username = $_SESSION['logged']; 

$update =("UPDATE site_user SET dateupdated = now(),ip = '$ip' WHERE 
username = '"($_SESSION['username'])."'"); 
$updated_once= $con->($update); 
$updated_once->execute(); 
} 

} 
?> 
+0

難道你不問[這個問題](http://stackoverflow.com/questions/15093251/how-to-change-an-update-query-from-mysql-to- pdo)已經有將近十幾次了? – 2013-03-02 11:05:15

+0

@YourCommonSense沒有更新,我問你一個我永遠不會回答的問題。這個騙局是不被認可的,我確實連接你教我的方式。並且在我的頁面中包含了連接頁面。它可以工作,但它似乎不能在函數中訪問 – humphrey 2013-03-02 11:47:19

回答

2

PDO可以有值綁定到參數。像這樣:

function updateonlinesession(){   
    if(isset($_SESSION['username']['id'])){ 
    $uid = $_SESSION['username']['id']; 
    $page = $_SERVER['REQUEST_URI']; 
    $ip = $_SERVER['REMOTE_ADDR']; 
    $username = $_SESSION['logged']; 
    $update = "UPDATE site_user SET dateupdated = now(), ip = ? WHERE username = ?"); 
    $updated_once = $con->prepare($update); 
    $updated_once->bindParam(1, $ip, PDO::PARAM_STR); 
    $updated_once->bindParam(2, $username, PDO::PARAM_STR); 
    $updated_once->execute(); 
    } 
} 
+0

回答謝謝你感謝的人 – humphrey 2013-03-02 10:59:13

+2

替代綁定,可以將它們作爲數組傳遞給'execute()',如下所示:'$ updated_once-> execute(array($ IP,$ username));' – hjpotter92 2013-03-02 11:00:37

+0

非常感謝的人,它似乎更容易使用數組 – humphrey 2013-03-02 11:01:53