我实际上是 PDO 的新手
在这里,我试图从 mysql 获取数据并以 xml 显示。
我已经使用 mysql 完成了它,但我无法使用 PDO 完成它。
这是我的PHP代码
<?php
error_reporting(E_ALL);
$host = "localhost";
$user = "root";
$pass = "root";
$database = "my_db";
// replace by a real *.xsl file, e.g.
// $xslt_file = "exam.xsl";
$xslt_file = FALSE;
// If true, will output XML without XSLT
$raw = TRUE;
$SQL_query = "SELECT * FROM `battery` order by waste asc";
$DB_link = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $DB_link) or die ("Could not find or access the database.");
$result = mysql_query ($SQL_query, $DB_link) or die ("Data not found. Your SQL query didn't work... ");
$left = "<";
$right = ">";
if ($xslt_file or $raw) {
// we produce XML
header("Content-type: text/xml");
$XML = "<?xml version=\"1.0\"?>\n";
if (!$raw) $XML .= "<?xml-stylesheet href=\"$xslt_file\" type=\"text/xsl\" ?>";
}
else {
// we produce HTML. All XML tags are replaced by printable entities
$XML = "Don't forget to create an XSLT file .... <p>";
$XML .= "<pre>\n";
$left = "<";
$right = ">";
}
// root node
$XML .= $left . "result" . $right . "\n";
// rows
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$XML .= "\t" . $left. "row" . $right . "\n"; // creates either "<row>" or "<row>"
$i = 0;
// cells
foreach ($row as $cell) {
// Escaping illegal characters
$cell = str_replace("&", "&", $cell);
$cell = str_replace("<", "<", $cell);
$cell = str_replace(">", ">", $cell);
$cell = str_replace("\"", """, $cell);
$col_name = mysql_field_name($result,$i);
// creates the "<tag>contents</tag>" representing the column, either as XML or for display in HTML
$XML .= "\t\t" . $left . $col_name . $right . $cell . $left . "/" . $col_name . $right ."\n";
$i++;
}
$XML .= "\t" . $left. "/row" . $right . "\n";
}
$XML .= $left . "/result" . $right . "\n";
echo $XML;
if (!$xslt_file && !$raw) echo "</pre>";
?>
我正在尝试很多,但我无法使用 PDO 完成它请我需要一些帮助。任何帮助将不胜感激。
我尝试过的 PDO 代码是
<?php
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "my_db";
$dbuser = "root";
$dbpass = "root";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$xslt_file = FALSE;
$raw = TRUE;
$SQL_query = "SELECT * FROM `battery` order by waste asc";
$result = $conn->query($SQL_query);
$left = "<";
$right = ">";
if ($xslt_file or $raw) {
header("Content-type: text/xml");
$XML = "<?xml version=\"1.0\"?>\n";
if (!$raw) $XML .= "<?xml-stylesheet href=\"$xslt_file\" type=\"text/xsl\" ?>";
}
else {
$XML = "Don't forget to create an XSLT file .... <p>";
$XML .= "<pre>\n";
$left = "<";
$right = ">";
}
$XML .= $left . "result" . $right . "\n";
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
$XML .= "\t" . $left. "row" . $right . "\n"; // creates either "<row>" or "<row>"
$i = 0;
// cells
foreach ($row as $cell) {
// Escaping illegal characters
$cell = str_replace("&", "&", $cell);
$cell = str_replace("<", "<", $cell);
$cell = str_replace(">", ">", $cell);
$cell = str_replace("\"", """, $cell);
$col_name = $result->fetchAll(PDO::FETCH_COLUMN);
// creates the "<tag>contents</tag>" representing the column, either as XML or for display in HTML
$XML .= "\t\t" . $left . $col_name . $right . $cell . $left . "/" . $col_name . $right ."\n";
$i++;
}
$XML .= "\t" . $left. "/row" . $right . "\n";
}
$XML .= $left . "/result" . $right . "\n";
echo $XML;
if (!$xslt_file && !$raw) echo "</pre>";
?>
但它什么也没显示