2011-10-12 10 views
0

私はmysqlにExcelデータをインポートする方法に関するチュートリアルを取り組んでいます。 問題はPEARをデータベース接続に使用していて、どのように動作するのかわかりません。だから私はコードを一般的に使用されるmysql接続文字列に変換したいと思います。 PEARやDB :: connectが以前に使われているのを見たことはないと確信しています。mysql接続用PHP PEARを変更してください

以下のコードがあります。私はあなたから想定し

<?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

しかし、あなたは「よく使われる」接続方法を知っていますか?必要なのは、好きなメソッドとの接続を作成し、 'add_person'関数を変更して挿入するだけです。 – Nanne

+2

* Gimme teh codez *のために閉じる投票。 – webbiedave

答えて

0

はあなたにもmysql_*機能し、新しくないMySQLiクラスをしたいことを投稿してください。 (私はOOインタフェースを好きなように個人的に、私は後者を好む。)

そして

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

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

を変更し

$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関数よりもきれいです)。

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

は次に、

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

があろう

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

あろう:

$res = mysqli_query($db,"INSERT INTO names VALUES ($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つのSさん)であることを保証することです。エンドユーザーが間違ったデータ型をフィールドに挿入していないことを確認するよい方法です。

残りの例では、XLSの値が読み込まれます。

+0

MDB2はほとんどの部分がDBの場所に直接滑り込むことができますが、一部の機能は直接同等/利用可能ではありません。正確には –

+0

です。 DBからMDB2への移行に関する優れたガイドは、ここにあります:http://www.phpied.com/db-2-mdb2/ –

関連する問題