从mysql php脚本转换为pdo

时间:2013-05-07 07:31:51

标签: php mysql xml pdo

我实际上是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>";

?> 

但它没有显示任何内容

2 个答案:

答案 0 :(得分:0)

获取列名称的正确方法是

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

然而,当您提取一个关联时,您可以将foreach循环更改为:

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

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

答案 1 :(得分: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:你不需要尝试和捕获,但我们过去常常得到错误并以一种很好的方式处理它

接下来我们这样查询:

 $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,并将结果集中列的值分配给它们所属的PHP变量   与PDOStatement :: bindColumn()方法绑定

  •   
  • 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 :返回一个匿名对象,其属性名与结果集中返回的列名相对应

  •