2012-02-12 84 views
-1

我想知道是否有人可以幫助我。PHP - 將PDO轉換爲正常代碼

我試圖將一些代碼集成到我的應用程序中,我需要集成的代碼是用PDO語句編寫的,我不知道它是如何發生的。

我想知道是否有人可以幫我轉換它。

的代碼如下

$sql = "insert into message2 (mid, seq, created_on_ip, created_by, body) values (?, ?, ?, ?, ?)"; 
$args = array($mid, $seq, '1.2.2.1', $currentUser, $body); 
$stmt = $PDO->prepare($sql); 
$stmt->execute($args); 
if (empty($mid)) { 
    $mid = $PDO->lastInsertId(); 
} 
$insertSql = "insert into message2_recips values "; 
$holders = array(); 
$params = array(); 
foreach ($rows as $row) { 
    $holders[] = "(?, ?, ?, ?)"; 
    $params[] = $mid; 
    $params[] = $seq; 
    $params[] = $row['uid']; 
    $params[] = $row['uid'] == $currentUser ? 'A' : 'N'; 
} 
$insertSql .= implode(',', $holders); 
$stmt = $PDO->prepare($insertSql); 
$stmt->execute($params); 
+2

PDO模塊如何不是「正常代碼」?你想把它轉換成什麼? – Carpetsmoker 2012-02-12 00:51:29

+0

推測從PDO到MySQL/MySQLi – Joe 2012-02-12 00:52:13

+0

噢,是的,對不起...... im用於mysql語句 – BigJobbies 2012-02-12 00:56:39

回答

0

您shoudl使用PDO unles你不能一些技術原因。如果你不知道,請學習它。也許這會讓你開始:

/* 
This the actual SQL query the "?" will be replaced with the values, and escaped accordingly 
- ie. you dont need to use the equiv of mysql_real_escape_string - its going to do it 
autmatically 
*/ 
$sql = "insert into message2 (mid, seq, created_on_ip, created_by, body) values (?, ?, ?, ?, ?)"; 

    // these are the values that will replace the ? 
    $args = array($mid, $seq, '1.2.2.1', $currentUser, $body); 

    // create a prepared statement object 
    $stmt = $PDO->prepare($sql); 

    // execute the statement with $args passed in to be used in place of the ? 
    // so the final query looks something like: 
    // insert into message2 (mid, seq, created_on_ip, created_by, body) values ($mid, $seq, 1.2.2.1, $currentUser, $body) 
    $stmt->execute($args); 


    if (empty($mid)) { 
     // $mid id is the value of the primary key for the last insert 
     $mid = $PDO->lastInsertId(); 
    } 

    // create the first part of another query 
    $insertSql = "insert into message2_recips values "; 

    // an array for placeholders - ie. ? in the unprepared sql string 
    $holders = array(); 

    // array for the params we will pass in as values to be substituted for the ? 
    $params = array(); 

    // im not sure what the $rows are, but it looks like what we will do is loop 
    // over a recordset of related rows and do additional inserts based upon them 
    foreach ($rows as $row) { 
     // add a place holder string for this row 
     $holders[] = "(?, ?, ?, ?)"; 

     // assign params 
     $params[] = $mid; 
     $params[] = $seq; 
     $params[] = $row['uid']; 
     $params[] = $row['uid'] == $currentUser ? 'A' : 'N'; 
    } 
    // modify the query string to have additional place holders 
    // so if we have 3 rows the query will look like this: 
    // insert into message2_recips values (?, ?, ?, ?),(?, ?, ?, ?),(?, ?, ?, ?) 
    $insertSql .= implode(',', $holders); 

    // create a prepared statment 
    $stmt = $PDO->prepare($insertSql); 

    // execute the statement with the params 
    $stmt->execute($params); 

PDO真的是更好。它具有與MySQLi相同的功能,但在數據庫驅動程序之間具有一致的接口(即只要您的SQL符合不同的數據庫,理論上可以使用與mysql,sqlite,postresql等完全相同的php代碼)AND AND預處理語句的參數綁定更好。既然你不應該以任何方式使用mysql擴展,並且MySQLi比PDO更加繁瑣,除非你明確地需要支持一個老版本的PHP,否則它真的是一件容易的事情。

+0

嘿,謝謝你.... yeh我不能使用這個項目的PDO語句的原因是即時通訊使用不使用PDO的框架 – BigJobbies 2012-02-12 01:38:54

+0

聽起來像你不應該使用該框架 - 它是哪一個? – prodigitalson 2012-02-12 01:40:51

+0

Im使用CodeIgniter – BigJobbies 2012-02-12 01:42:06