0

Hi guys I am trying to write simple program that extracts data from database using PDO, save it as multidimensional array and then convert it to XML.

The problem is that I cannot have numeric key that contains zero, because I am using Array2XML class for conversion and this class converts all keys to xml tags (and error tells me that tag with 0 inside is illegal).

So is there any way to extract data from database into multidimensional array and avoid getting 0 (I do not mind skipping using numeric keys at all)? Here is my code:

<?php

require 'config.php';
require 'array2xml.php';

//get username and password
$config = new Config();

$user = $config->getUser();
$pass = $config->getPass();

//create connection object
try {

    $dbc = new PDO('mysql:host=localhost;dbname=StudentDB',
        $user, $pass);
    echo 'Connection established <br>';
}

catch(PDOException $e) {
    echo "An error occured while connecting to database: "
        . $e->getMessage();
}

$sql = $dbc->query('SELECT * FROM student');

$result = $sql->fetchALL(PDO::FETCH_CLASS);

echo "<pre>" . print_r($result, true) . "<pre/>";

echo "<br>";
echo "<br>";

//create object of class capable of conversion
$convert = new Array2XML();

$xml = $convert::createXML("Student", $result);

$xmlstring = $xml->saveXML();

echo "<pre>" . $xmlstring . "<pre/>";

I would like to seek your wisdom and ask for help. Thank you

Here is the proof that my question is different from:

PDO fetchAll group key-value pairs into assoc array

  1. In this question you are linking to he is not sure about his mysql query - he is not selecting entire table in raw format - and I am!
  2. His question has nothing to do with xml - NOTHING!
  3. If I am correct he is one step ahead of me so there is no way that solution for his problem wll work for me.
Community
  • 1
  • 1
XSorcery
  • 83
  • 1
  • 10
  • possible duplicate of [PDO fetchAll group key-value pairs into assoc array](http://stackoverflow.com/questions/7451126/pdo-fetchall-group-key-value-pairs-into-assoc-array) – Jay Blanchard Aug 03 '15 at 21:29
  • What would you like to have as keys in $result array? – jedrzej.kurylo Aug 03 '15 at 22:21
  • I don't mind anything in the key as long as it can be converted to a legal xml tag (so it cannot be 0). – XSorcery Aug 03 '15 at 23:00
  • Jay - I am inexperienced and I could do not understood how my question related to this one. Yet I am pretty sure they have nothing in common except the same language. So every question about php/mysql is a duplicate of other one? I can assume that judging from your accusation. 1. In this question you are linking to he is not sure about his mysql query - he is not selecting entire table in raw format - and I am! 2. His question has nothing to do with xml - NOTHING! 3. If I am correct he is one step ahead of me so there is no way that solution for his problem wll work for me. – XSorcery Aug 03 '15 at 23:08
  • Jay - I am pretty sure that you didn't even read my question - you just compared title, tags and said to yourself that they both use pdo so it must be a duplicate. Is blaming people for something that you are not sure they did make you feel better? And it does not helped that you said ,,possible duplicate" - because it does not make any sense. Following this logic every person that is capable of moving an item is a ,,possible thief". This is a big lie - people either steal or does not steal - simply true || false. So start thinking before you do - moderators will have less work too... – XSorcery Aug 03 '15 at 23:14
  • There is no built in option for pdo->fetchAll() to alter the result array. In your case you could use array_unshift() as a workaround to set a new element at index 0 an use unset($result[0]) to remove it again. Then your array starts at Index 1... Not really elegant i know. – Tarsis Aug 04 '15 at 13:08

0 Answers0