Note that there are some explanatory texts on larger screens.

plurals
  1. POconvert from mysql php script to pdo
    text
    copied!<p>I am actually new to PDO</p> <p>Here I am trying to fetch data from mysql and show in xml.</p> <p>I have done it using mysql, but I could not be able to done it using PDO.</p> <p>Here is my PHP code</p> <pre><code>&lt;?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 = "&lt;"; $right = "&gt;"; if ($xslt_file or $raw) { // we produce XML header("Content-type: text/xml"); $XML = "&lt;?xml version=\"1.0\"?&gt;\n"; if (!$raw) $XML .= "&lt;?xml-stylesheet href=\"$xslt_file\" type=\"text/xsl\" ?&gt;"; } else { // we produce HTML. All XML tags are replaced by printable entities $XML = "Don't forget to create an XSLT file .... &lt;p&gt;"; $XML .= "&lt;pre&gt;\n"; $left = "&amp;lt;"; $right = "&amp;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 "&lt;row&gt;" or "&amp;lt;row&amp;gt;" $i = 0; // cells foreach ($row as $cell) { // Escaping illegal characters $cell = str_replace("&amp;", "&amp;amp;", $cell); $cell = str_replace("&lt;", "&amp;lt;", $cell); $cell = str_replace("&gt;", "&amp;gt;", $cell); $cell = str_replace("\"", "&amp;quot;", $cell); $col_name = mysql_field_name($result,$i); // creates the "&lt;tag&gt;contents&lt;/tag&gt;" 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 &amp;&amp; !$raw) echo "&lt;/pre&gt;"; ?&gt; </code></pre> <p>I am trying a lot, but i could not be able to done it using PDO Please i need some help. Any Help will be appreciated.</p> <p>My PDO code that i tried is </p> <pre><code> &lt;?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-&gt;query($SQL_query); $left = "&lt;"; $right = "&gt;"; if ($xslt_file or $raw) { header("Content-type: text/xml"); $XML = "&lt;?xml version=\"1.0\"?&gt;\n"; if (!$raw) $XML .= "&lt;?xml-stylesheet href=\"$xslt_file\" type=\"text/xsl\" ?&gt;"; } else { $XML = "Don't forget to create an XSLT file .... &lt;p&gt;"; $XML .= "&lt;pre&gt;\n"; $left = "&amp;lt;"; $right = "&amp;gt;"; } $XML .= $left . "result" . $right . "\n"; while($row = $result-&gt;fetch(PDO::FETCH_ASSOC)) { $XML .= "\t" . $left. "row" . $right . "\n"; // creates either "&lt;row&gt;" or "&amp;lt;row&amp;gt;" $i = 0; // cells foreach ($row as $cell) { // Escaping illegal characters $cell = str_replace("&amp;", "&amp;amp;", $cell); $cell = str_replace("&lt;", "&amp;lt;", $cell); $cell = str_replace("&gt;", "&amp;gt;", $cell); $cell = str_replace("\"", "&amp;quot;", $cell); $col_name = $result-&gt;fetchAll(PDO::FETCH_COLUMN); // creates the "&lt;tag&gt;contents&lt;/tag&gt;" 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 &amp;&amp; !$raw) echo "&lt;/pre&gt;"; ?&gt; </code></pre> <p>But it shows nothing</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload