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>
하지만 '일반적으로 사용되는'연결 방법을 알고 있습니까? 당신이해야 할 일은 가장 좋아하는 메소드와의 연결을 만든 다음 insert를하기 위해'add_person' 함수를 변경하는 것입니다. – Nanne
* Gimme teh codez *로 투표 마감. – webbiedave