2013-02-26 54 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']; 

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

    } 

這是我試圖與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 = '$ip' WHERE 
username = '".($_SESSION['username'])."'"); 
$sth_update= $con->prepare($update); 
$sth_update->execute(); 
} 

} 

?> 
+2

發表你試過的東西... – Eggplant 2013-02-26 15:42:35

+1

有一個gander:http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – Nick 2013-02-26 15:43:16

+0

這個線程談論'sql注入',但它顯示了使用準備語句的方法,其中一個是PDO,[如何防止SQL注入在PHP?](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php) – 2013-02-26 15:43:42

回答

3
$stmt = $db->prepare("UPDATE site_user SET dateupdated=now(), ip=? WHERE username=?"); 
$stmt->execute(array($ip, $_SESSION['username'])); 
$affected_rows = $stmt->rowCount(); 

其中$db是你的連接

再次:A great tutorial

+0

大先生尼克良好的工作的人,我真的很感激 – humphrey 2013-02-26 15:51:25

+0

沒問題,交換機好運,相信我值得學習PDO。而且這個教程有很多幫助。 – Nick 2013-02-26 15:52:18

5
// connetion 
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); 

// query 
$sql = "UPDATE site_user SET dateupdated = NOW(), ip = ? 
     WHERE username = ?"; 
$q = $conn->prepare($sql); 
$q->execute(array($ip, $_SESSION['username']); 
+0

你這人非常感謝的人 – humphrey 2013-02-26 15:48:56

5

首先,在引導/ config文件中的某個地方連接:

$dsn = 'mysql:dbname=test;host=localhost;charset=utf8'; 
$opt = array(
    PDO::ATTR_ERRMODE   => PDO::ERRMODE_EXCEPTION, 
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC 
); 
$pdo = new PDO($dsn, $user, $pass, $opt); 

然後,運行查詢

function updateonlinesession(){ 
    global $pdo; 
    if(isset($_SESSION['username']['id'])){ 
     $sql = "UPDATE site_user SET dateupdated=now(),ip=? WHERE username=?"; 
     $pdo->prepare($sql); 
     $pdo->execute(array($_SERVER['REMOTE_ADDR'], $_SESSION['logged'])); 
    } 
} 

確保你使用的所有變量具有正確的值(var_dump($_SESSION)就夠了)

+0

你真正的明星ü總是幫助我偉人 – humphrey 2013-02-26 15:53:36

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

     //Establish your PDO object. 
     $pdo = new PDO("mysql:host=localhost;dbname=mydb", "username", "password"); 

     //Prepare your statement. 
     $stmt = $pdo->prepare("UPDATE site_user SET dateupdate = now(), ip = ? WHERE username = ?"); 
     $stmt->execute(array(
      $ip, 
      $_SESSION['username'] 
     )); 
     $stmt->closeCursor(); 
    } 
} 
+0

讚賞非常感謝,我們需要人們像你 – humphrey 2013-02-26 15:55:24