2012-02-12 10 views
-1

誰かが私を助けてくれるのだろうかと思っていました。PHP - PDOを通常のコードに変換する

私のアプリケーションにいくつかのコードを統合しようとしていますが、統合する必要があるコードはPDOステートメントで書かれています。

私は誰かがそれを変換するのを助けることができたのだろうかと思っていました。あなたはカントいくつかの技術的な理由でPDOのunlesを使用shoudl

$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

+0

おそらくPDOからMySQL/MySQLi – Joe

+0

ああ、申し訳ありません...私はmysqlステートメントに使用されています – BigJobbies

答えて

0

を次のように

コードがあります。あなたがそれを知らないなら、それを学んでください。たぶんこれがあなたを起動させます:

/* 
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と同じ機能を持ちますが、DBドライバ間で一貫性のあるインターフェイスを備えています(つまり、SQLが異なるデータベースに準拠している限り、理論的にはmysql、sqlite、postresqlなどと全く同じPHPコードを使用できます)プリペアドステートメントに対するより良いパラメーターバインディングあなたはmysqlの拡張子を使うべきではないので、PHPの古いバージョンを特別にサポートしなければならない限り、MySQLiはPDOよりも扱いが面倒です。

+0

こんにちは、ありがとうございました.... yeh理由は私はこのプロジェクトのPDOステートメントを使用するPDOを使用してフレームワークを使用しています – BigJobbies

+0

あなたはそのフレームワークを使用するべきではないような音 - どちらですか? – prodigitalson

+0

Im CodeIgniterを使用しています – BigJobbies

関連する問題