Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

issue with database wrapper (mySQL to XML)

Status
Not open for further replies.

Durgles

Programmer
Apr 28, 2008
1
Hey guys, I've been banging my head against the desk for the last hour trying to figure this out, and I can't. I'm primarally a Flex developer, so PHP is not my first language, if you will.

The purpose of the script is to pull records dynamically from mySQL so that my Flex application can read it, but it's not handling text fields. Any database entry with type text is coming up blank.

Code:
<?php
// Make a MySQL Connection
  require_once("db_info.php");

  $database = $_POST['DB'];
  $table = $_POST['Table'];
  $fields = $_POST['Fields'];
  $where = $_POST['Where'];
  $orderBy = $_POST['OrderBy'];

  $link = mysql_connect($sqlServer,$sqlUsername,$sqlPW) or killme("0993", "Unable to connect to Database Server", $SQL, $database);
  $table = mysql_real_escape_string($table);
  $fields = mysql_real_escape_string($fields);
  $where = mysql_real_escape_string($where);
  $orderBy = mysql_real_escape_string($orderBy);

  $SQL = "DESCRIBE ".$table.";";
  @mysql_select_db($database) or killme("4414", "Unable to Select DB ".$database, $SQL, $database);
  $result = mysql_query($SQL,$link) or killme("4415", "Unable to Describe Table", $SQL, $database);
  $numrows = mysql_numrows($result);
  if ($numrows == 0) killme("4928", "No rows retrieved on Describe.", $SQL);

  $doc = new DomDocument('1.0');

  $root = $doc->createElement('entries');
  $root = $doc->appendChild($root);

  while($row = mysql_fetch_assoc($result)) {
    $occ = $doc->createElement("attribute");
    $occ = $root->appendChild($occ);
    foreach ($row as $fieldname => $fieldvalue) {
      $child = $doc->createElement($fieldname);
      $child = $occ->appendChild($child);
      $value = $doc->createTextNode($fieldvalue);
      $value = $child->appendChild($value);
    }
  }

  if ($fields == "") $fields = "*";

  $SQL = "SELECT ".$fields." FROM ".$table;
  if ($where != "") $SQL = $SQL . " WHERE " . $where;
  if ($orderBy != "") $SQL = $SQL . " ORDER BY " . $orderBy;
  $result2 = mysql_query($SQL,$link) or killme("4515", "Unable to Select from Table", $SQL);

  $numrows = mysql_numrows($result2);
  if ($numrows == 0) killme("0", "No rows retrieved.", $SQL);

  while($row = mysql_fetch_assoc($result2)) {
    $occ = $doc->createElement("entry");
    $occ = $root->appendChild($occ);
    foreach ($row as $fieldname => $fieldvalue) {
      $child = $doc->createElement($fieldname);
      $child = $occ->appendChild($child);
      $value = $doc->createTextNode($fieldvalue);
      $value = $child->appendChild($value);
    }
  }

  $occ = $doc->createElement("error");
  $occ = $root->appendChild($occ);
  $child = $doc->createElement("code");
  $child = $occ->appendChild($child);
  $value = $doc->createTextNode("0");
  $value = $child->appendChild($value);
  $child = $doc->createElement("rowcount");
  $child = $occ->appendChild($child);
  $value = $doc->createTextNode(mysql_numrows($result));
  $value = $child->appendChild($value);
  $xml_string = @$doc->saveXML();

  echo $xml_string;
  mysql_close($link);
?>

Help. x_x

Edit: Yes, I have checked the database; everything is fine and the data IS there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top