-1

我实际上是 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 = "&lt;";
   $right = "&gt;";
 }

// 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 "&lt;row&gt;"
  $i = 0;
  // cells
  foreach ($row as $cell) {
    // Escaping illegal characters
    $cell = str_replace("&", "&amp;", $cell);
    $cell = str_replace("<", "&lt;", $cell);
    $cell = str_replace(">", "&gt;", $cell);
    $cell = str_replace("\"", "&quot;", $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 = "&lt;";
   $right = "&gt;";
 }


  $XML .= $left . "result" . $right . "\n";

  while($row = $result->fetch(PDO::FETCH_ASSOC)) {  
  $XML .= "\t" . $left. "row" . $right . "\n";  // creates either "<row>" or "&lt;row&gt;"
  $i = 0;
  // cells
  foreach ($row as $cell) {
    // Escaping illegal characters
    $cell = str_replace("&", "&amp;", $cell);
    $cell = str_replace("<", "&lt;", $cell);
    $cell = str_replace(">", "&gt;", $cell);
    $cell = str_replace("\"", "&quot;", $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>";

?> 

但它什么也没显示

4

2 回答 2

0

你像这样连接到数据库:

$dbhost = "localhost";
$dbname = "testcreate";
$dbuser = "root";
$dbpass = "mysql";

try {
$db = new PDO('mysql:host='.$dbhost.';dbname='.$dbname.';charset=utf-8', ''.$dbuser.'', ''.$dbpass.'');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch (PDOException $e) {
       echo 'Error : <br>' . $e->getMessage();
}

PS:你不需要 try 和 catch,但我们曾经得到错误并以我们想要的方式处理它

接下来我们这样查询:

 $db->query(SELECT * FROM node WHERE node_name='$nodename'");

我们像这样获取它:

 $query = $db->query(SELECT * FROM node WHERE node_name='$nodename'");
 $row   = $query->fetch(PDO::FETCH_OBJ);

现在你使用$row->name例如

这里有更多关于 PDO::FETCH

  • PDO::FETCH_ASSOC:返回一个按列名索引的数组,如结果集中返回的那样

  • PDO::FETCH_BOTH(默认):返回一个由列名和 0 索引列号索引的数组,如结果集中返回的那样

  • PDO::FETCH_BOUND:返回 TRUE 并将结果集中列的值分配给
    使用 PDOStatement::bindColumn() 方法绑定到的 PHP 变量

  • PDO::FETCH_CLASS:返回所请求类的新实例,将结果集的列映射到
    类中的命名属性。如果 fetch_style 包括 PDO::FETCH_CLASSTYPE(例如
    PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE),那么类的名称
    由第一列的值确定。

  • PDO::FETCH_INTO:更新请求类的现有实例,将结果集的列映射到
    类中的命名属性

  • PDO::FETCH_LAZY : 结合 PDO::FETCH_BOTH 和 PDO::FETCH_OBJ,在访问对象变量名时创建它们

  • PDO::FETCH_NUM:返回一个按列号索引的数组,如结果集中返回的那样,从第 0 列开始

  • PDO::FETCH_OBJ:返回一个匿名对象,其属性名称对应于结果集中返回的列名

于 2013-07-10T02:41:11.727 回答
0

获取列名的正确方法是

$q = $dbh->prepare("DESCRIBE tablename");
$q->execute();
$table_fields = $q->fetchAll(PDO::FETCH_COLUMN);

但是,当您获取 assoc 时,您可以将 foreach 循环更改为:

foreach ($row as $col_name => $cell) {

可能还有其他问题,您是否尝试过在每个 PDO 函数调用的结果上使用 print_r 来检查它在什么时候失败?

于 2013-05-08T12:53:30.613 回答