Error generating XML from a MySQL database

0

I need to generate XML from a MySQL database. I am using MySQL database with PHP (which makes it clear that I am new to PHP, what I did was with a YouTube video ( this ) 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.PHP for XML \ config.php: 35 Stack trace: # 0 { main} thrown in C: \ xampp \ htdocs \ php_fundamental \ 2.PHP for XML \ config.php on line 35

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

I would like help to finish.

    
asked by anonymous 07.12.2018 / 20:07

1 answer

1

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

This happens because you are creating the element in the wrong way here:

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

The right thing to do is:

$produtos = $xml->createElement('Produtos');
    
07.12.2018 / 21:22