2011-10-12 5 views
0

mysql으로 Excel 데이터를 가져 오는 방법에 대한 자습서와 함께 작업하고 있습니다. 내가 가지고있는 문제는 그들이 PEAR을 사용했다는 것입니다. 데이터베이스 연결을 위해 어떻게 작동하는지 모르겠습니다. 그래서 일반적으로 사용되는 mysql 연결 문자열로 코드를 변환하고 싶습니다. 나는 PEAR 또는 DB :: connect가 전에 사용 된 것을 본 적이 없다고 확신합니다.mysql 연결을위한 PHP PEAR 변경

다음 코드는 다음과 같습니다. 당신은 또한 mysql_* 기능이 아닌 새로운 MySQLi 클래스를 원하는 게시에서

<?php 
require_once("db.php"); 

$data = array(); 

$db =& DB::connect("mysql://[email protected]/names", array()); 
if (PEAR::isError($db)) { die($db->getMessage()); } 

function add_person($first, $middle, $last, $email) 
{ 
global $data, $db; 

$sth = $db->prepare("INSERT INTO names VALUES(0, ?, ?, ?, ?)"); 
$db->execute($sth, array($first, $middle, $last, $email)); 

$data []= array(
    'first' => $first, 
    'middle' => $middle, 
    'last' => $last, 
    'email' => $email 
); 
} 

if ($_FILES['file']['tmp_name']) 
{ 
$dom = DOMDocument::load($_FILES['file']['tmp_name']); 
$rows = $dom->getElementsByTagName('Row'); 
$first_row = true; 
foreach ($rows as $row) 
{ 
    if (!$first_row) 
    { 
    $first = ""; 
    $middle = ""; 
    $last = ""; 
    $email = ""; 

    $index = 1; 
    $cells = $row->getElementsByTagName('Cell'); 
    foreach($cells as $cell) 
    { 
     $ind = $cell->getAttribute('Index'); 
     if ($ind != null) $index = $ind; 

     if ($index == 1) $first = $cell->nodeValue; 
     if ($index == 2) $middle = $cell->nodeValue; 
     if ($index == 3) $last = $cell->nodeValue; 
     if ($index == 4) $email = $cell->nodeValue; 

     $index += 1; 
    } 
    add_person($first, $middle, $last, $email); 
    } 
    $first_row = false; 
} 
} 
?> 
<html> 
<body> 
These records have been added to the database: 
<table> 
<tr> 
<th>First</th> 
<th>Middle</th> 
<th>Last</th> 
<th>Email</th> 
</tr> 
<?php foreach($data as $row) { ?> 
<tr> 
<td><?php echo($row['first']); ?></td>< 
<td><?php echo($row['middle']); ?></td>< 
<td><?php echo($row['last']); ?></td>< 
<td><?php echo($row['email']); ?></td>< 
</tr> 
<?php } ?> 
</table> 
Click <a href="list.php">here</a> for the entire table. 
</body> 
</html> 
+1

하지만 '일반적으로 사용되는'연결 방법을 알고 있습니까? 당신이해야 할 일은 가장 좋아하는 메소드와의 연결을 만든 다음 insert를하기 위해'add_person' 함수를 변경하는 것입니다. – Nanne

+2

* Gimme teh codez *로 투표 마감. – webbiedave

답변

0

나는 가정합니다. (나는 OO 인터페이스를 좋아 개인적으로, 나는 후자를 선호합니다.)

그런 다음

$db =& DB::connect("mysql://[email protected]/names", array()); 
if (PEAR::isError($db)) { die($db->getMessage()); } 

으로
$db = @mysql_connect('localhost', 'user', 'pass') or die (mysql_error()); 
mysql_select_db('db_name', $db); 

을 변경하고 변경

$sth = $db->prepare("INSERT INTO names VALUES(0, ?, ?, ?, ?)"); 
$db->execute($sth, array($first, $middle, $last, $email)); 

$query = sprintf( 
     'INSERT INTO names VALUES(0, "%s", "%s", "%s", "%s")' , 
     mysql_real_escape_string($first), 
     mysql_real_escape_string($middle), 
     mysql_real_escape_string($last), 
     mysql_real_escape_string($email) 
    ); 

mysql_query($query, $db); 
+0

그 덕분에 많은 도움이되었습니다. 아무 것도 변경할 필요조차 없었습니다. 방금 코드를 복사하여 붙여 넣었습니다. –

0

PEAR DB는 가치가 떨어지는 데이터베이스 추상화 계층입니다. 실제로 사용하기가 간단합니다 (실제로는 기본 mysql 함수보다 더 깨끗합니다).

$res = mysqli_query($db,"INSERT INTO names VALUES ($first, $middle, $last, $email)"); 

이 매우 단순화 (잠재적으로 위험한) 예이다

$db =& DB::connect("mysql://[email protected]/names", array()); 
if (PEAR::isError($db)) { die($db->getMessage()); } 

$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname)) or die("The site database appears to be down."); 

다음

$sth = $db->prepare("INSERT INTO names VALUES(0, ?, ?, ?, ?)"); 
$db->execute($sth, array($first, $middle, $last, $email)); 

가 될 것이다. PEAR DB가 제공하는 내장 보호 장치 (준비된 명령문)를 잃어 버렸음에 유의하십시오. 다음과 같이 기본 MySQL의 함수를 사용하여 위의 문을 수정할 수 :

$res = mysqli_prepare($db, "INSERT INTO names VALUES(0, ?, ?, ?, ?)") 
mysqli_stmt_bind_param($res, 'ssss', $first, $middle, $last, $email); 
mysqli_stmt_execute($res); 
mysqli_stmt_close($res); // CLOSE $res 

두 번째 예제를 사용의 장점은 MySQL이 네 개의 필드는 문자열합니다 (bind_param 기능의 4 개의의의)하도록 보장한다는 것입니다. 최종 사용자가 필드의 잘못된 데이터 형식을 삽입하지 않도록하는 좋은 방법입니다.

나머지 예제는 XLS에서 값을 읽습니다.

+0

MDB2의 대부분은 DB의 위치로 밀어 넣을 수 있지만 일부 기능은 직접 사용할 수 없습니다. –

+0

정확합니다. DB에서 MDB2 로의 마이그레이션에 대한 좋은 안내서는 다음과 같습니다. http://www.phpied.com/db-2-mdb2/ –