So I've been tasked with moving this php script and some other content from MySQL over to DB2, I'm not proficent with PHP and was hoping someone could help me with this bugger. It's giving me some issues.
Basically what I need this to do is query the db2 database from the sql POST variable. It should then return the results in xml. This script is currently working with MySQL but DB2 seems like another beast all together. Hopefully someone can help.
Basically what I need this to do is query the db2 database from the sql POST variable. It should then return the results in xml. This script is currently working with MySQL but DB2 seems like another beast all together. Hopefully someone can help.
Code:
<?php
$mysql_sql=$_POST['sql'];
$mysql_hostname = *;
$mysql_user = *;
$mysql_password = *";
$mysql_database = *;
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");
// Opens a connection to a mySQL server
$connection=mysql_connect ($mysql_hostname, $mysql_user, $mysql_password);
if (!$connection) {
die("Not connected : " . mysql_error());
}
$dom = new DOMDocument("1.0");
$node = $dom->createElement("rows");
$parnode = $dom->appendChild($node);
// Set the active MySQL database
$db_selected = mysql_select_db($mysql_database, $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
// Select all the rows in query
$query = $mysql_sql;
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
header("Content-type: text/xml");
// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
// ADD TO XML DOCUMENT NODE
$node = $dom->createElement("row");
$newnode = $parnode->appendChild($node);
for($i = 0; $i < mysql_num_fields($result); $i++)
{
$field_info = mysql_fetch_field($result, $i);
$newnode->setAttribute($field_info->name, $row[$field_info->name]);
}
}
echo $dom->saveXML();
?>