2011-10-12 54 views
0

我正在与一些如何将Excel数据导入到MySQL的教程。 我遇到的问题是他们使用PEAR:用于数据库连接,我不知道它是如何工作的。所以我想将代码转换为常用的mysql连接字符串。我相信我从未见过以前使用过的PEAR或DB :: connect。更改php梨为mysql连接

下面是下面的代码。

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

投票结束是因为*给我的codez *。 – webbiedave

回答

0

我想从您发布,你还想要mysql_*功能,而不是新的MySQLi类。 (个人而言,我更喜欢后者作为我喜欢的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函数更清洁)。

$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)); 

将是:

$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功能的四个S的)。确保最终用户不会为该字段插入错误数据类型的好方法。

您的示例的其余部分只是从XLS读取值。

+0

尽管有些函数不是直接等价/可用,但MDB2大多数情况下可以直接进入DB的位置。确切地说, –

+0

。有关从数据库迁移到MDB2的好指南,请访问:http://www.phpied.com/db-2-mdb2/ –