使用pdo的独立示例
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
$stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
$stmt->bindParam('name', $name);
$stmt->bindParam('age', $age);
$data = data();
// number of elements in $data['guest']
echo count($data['guest']), " new entries<br />\n";
// add data to mysql database
// iterate over elements in $data['guest']
foreach( $data['guest'] as $row ) {
// assign "values" to the previously bound parameters
$name = $row['name'];
$age = $row['age'];
// execute preapred statement
$stmt->execute();
}
$stmt = null;
// retrieve data: only count
// if you only want count the records do not transfer the data from the mysql server to your php process - use Count(*) instead
$row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
echo $row[0], " entries in database<br />\n";
// retrieve data: actual data
$stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
echo $row['name'], ' ', $row['age'], "<br />\n";
}
// stmt->rowCount() after a SELECT statement doesn't work with all pdo drivers, but for PDO_MySQL it does
echo $stmt->rowCount(), ' records fetched';
// boilerplate: create temporary database structure ...
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
name varchar(32),
age int,
primary key(id)
)
');
}
// boilerplate: a function returning example data to work on...
function data() {
return array (
'guest' => array (
array (
'name' => 'Joseph Needham',
'age' => 53
),
array (
'name' => 'Lu Gwei-djen',
'age' => 31
)
)
);
}
编辑:给定您的 xml,您可以执行类似的操作
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
setup($pdo);
$stmt = $pdo->prepare('INSERT INTO soFoo (name, age) VALUES (:name, :age)');
$stmt->bindParam('name', $name);
$stmt->bindParam('age', $age);
// insert data
$data = data();
echo count($data->guest), " new entries<br />\n";
foreach( $data->guest as $guest ) {
$name = (string)$guest->name;
$age = (string)$guest->age;
$stmt->execute();
}
$stmt = null;
// retrieve data: only count
$row = $pdo->query('SELECT Count(*) from soFoo')->fetch();
echo $row[0], " entries in database<br />\n";
// retrieve data: actual data
$stmt = $pdo->query('SELECT name, age from soFoo', PDO::FETCH_ASSOC);
foreach( $stmt as $row ) {
echo $row['name'], ' ', $row['age'], "<br />\n";
}
echo $stmt->rowCount(), ' records fetched';
function setup($pdo) {
$pdo->exec('
CREATE TEMPORARY TABLE soFoo (
id int auto_increment,
name varchar(32),
age int,
primary key(id)
)
');
}
function data() {
// return simplexml_load_file('...');
return new SimpleXMLElement( <<< eox
<?xml version="1.0" encoding="utf-8"?>
<everyone>
<guest>
<name>Joseph Needham</name>
<age>53</age>
</guest>
<guest>
<name>Lu Gwei-djen</name>
<age>31</age>
</guest>
</everyone>
eox
);
}
输出是一样的。
但请记住 simplxml_load_file() 将整个 DOM 保存在内存中。如果您的数据源变得很大,这可能会成为问题,您最好切换到XMLReadery,