2013-05-07 58 views
-1

我其實是PDO的新手從MySQL的PHP​​腳本轉換爲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>"; 

?> 

但它說明不了什麼

+0

哪裏' PDO'代碼,這是所有'mysql'代碼。 – 2013-05-07 07:34:32

+0

@YogeshSuthar:\t <?php \t while($ row = $ result-> fetch(PDO :: FETCH_ASSOC)){ \t $ XML。=「\ t」。 $離開。 「行」。 $正確。 「\ n」 個; //創建「」或「<」行>「 \t $ i = 0; \t //細胞 \t的foreach($行作爲$細胞){ \t //轉義非法字符 \t $細胞= str_replace函數( 「&」, 「&」,$細胞); \t $ cell = str_replace(「<」,「<」,$ cell); \t $ cell = str_replace(「>」,「>」,$ cell); \t $細胞= str_replace函數( 「\」」, 「"」,$細胞); \t $ COL_NAME = $ result->使用fetchall(PDO :: FETCH_COLUMN); \t> – 2013-05-07 07:38:18

+3

粘貼到你的問題這個代碼不 – 2013-05-07 07:39:30

回答

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來檢查它在哪個點失敗?

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約束:返回所請求的類的新實例,映射將結果集的列設置爲命名屬性s在
    類。如果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:作爲返回我返回由列號索引的數組n您的結果集,開始於第0列

  • PDO :: FETCH_OBJ:返回與屬性名稱的匿名對象對應於列名在結果集返回

+0

Dude。不要發送你的複製粘貼的答案給你找到的每一個問題。 – 2013-07-10 06:32:07