Error generating XML from a Mysql database

Asked

Viewed 79 times

0

I need to generate XML from a Mysql database. I’m using Mysql database with PHP (let’s be clear that I’m new to PHP, what I did was with a Youtube video (that) and very little of what I know).

<?php

    define('HOSTNAME', '127.0.0.1');
    define('USERNAME', 'root');
    define('PASSWORD', null);
    define('DATABASE', 'cadastro');
    define('CHARSET' , 'utf8');


    include_once("conexao.php");

    $sql = 'select id, descricao, margem, custo, estoque from produtos';

    $resultado = mysqli_query(DBConnect(), $sql) or die (mysqli_error(DBConnect()));

    $xml = new DOMDocument('1.0', 'ISO-8859-1');
    $xml->preserveWhiteSpace = false;
    $xml->formatOutput = true;

    $produtos = $xml->createElement=('Produtos');

    while($dados = mysqli_fetch_object($resultado))
    {
        $item = $xml->createElement('Item');
        $descricao = $xml->createElement('descricao', $dados->descricao);
        $margem = $xml->createElement('margem', $dados->margem);
        $custo = $xml->createElement('custo', $dados->custo);
        $estoque = $xml->createElement('estoque', $dados->estoque);

        $item->appendChild($descricao);
        $item->appendChild($margem);
        $item->appendChild($custo);
        $item->appendChild($estoque);

        $produtos->appendChild($item);
    }

    $xml->appendChild($produtos);

    header('content-type: text/xml');
    print $xml->saveXML();

?>

The error that gives:

Fatal error: Uncaught Error: Call to a Member Function appendchild() on string in C: xampp htdocs php_fundamental 2.Phpparaxml config.php:35 Stack trace: #0 {main} thrown in C: xampp htdocs php_fundamental 2.Phppaml raxconfig.php on line 35

(The line 35 that is said in error is rightly: $produtos->appendChild($item);)

I’d like help finishing.

1 answer

1


This error means that you are calling a function from a variable that is not an object.

This is because you are creating the element wrong here:

$produtos = $xml->createElement=('Produtos');

The right thing is:

$produtos = $xml->createElement('Produtos');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.