2012-02-12 3 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 코드를 사용할 수 있음)와 일관된 인터페이스를 제공합니다. prepared statement에 대한 더 나은 매개 변수 바인딩. 이후로 당신은 어떤 식 으로든 mysql 확장을 사용하지 않아야하고, MySQLi는 PHP의 이전 버전을 특별히 지원하지 않는 한 PDO보다 다루기가 더 어렵습니다.

+0

이봐 요, 고마워요 .... 예, 왜이 프로젝트에 PDO 문을 사용할 수없는 이유는 메신저 PDO를 사용하지 않는 프레임 워크를 사용하는 것입니다 – BigJobbies

+0

그런 프레임 워크를 사용해서는 안되는 것처럼 들리겠습니까? – prodigitalson

+0

Im Using CodeIgniter – BigJobbies