简体   繁体   中英

Error query SQL parsing XML in PHP

I get the following error trying to traverse the xml and inserting each node in the DB.

Notice: Trying to get property of non-object

the lines of the error are these:

$ninv= $obra->ninventario."/".$obra->ncatalogo;

$consulta ="INSERT INTO bcficha ( ninv , contrato , desc , tecnica , pieza , dimen , lochab , ubicacion ) VALUES ('".$ninv."','".$obra->contrato."','".$obra->titulo."','".$obra->tecnica."','".$obra->tipo."','".$obra->medidas_con_marco."', '".$obra->localizacion_habitual.", '".$obra->ultima_localizacion."'); ";

The XML file goes through it perfectly because I have comporbado with an echo and one of the parameters of the node.

I do not know if it's a problem with the connection with the BD, which I think not, or it's a problem of another type.

<?php

$fichero = "./obras.xml";
if(!$xml = simplexml_load_file($fichero))
{
    echo "No se ha podido cargar el archivo <br>";
} 
else 
{
    echo "El archivo se ha cargado correctamente <br>";
}

include"./conectar.php";
$enlace= conectarse();

foreach ($xml as $obra)
{
    $obra = (array) $obra;
    $ninv= $obra->ninventario."/".$obra->ncatalogo;

    $consulta ="INSERT INTO `bcficha` (`ninv`, `contrato`, `desc`, `tecnica`, `pieza`, `dimen`, `lochab`, `ubicacion`) VALUES ('".$ninv."','".$obra->contrato."','".$obra->titulo."','".$obra->tecnica."','".$obra->tipo."','".$obra->medidas_con_marco."', '".$obra->localizacion_habitual.", '".$obra->ultima_localizacion."'); ";

    $resultado= mysqli_query($enlace,$consulta) or die (mysqli_error);
    if ($resultado)
    {
        echo ("Base de datos caragda correctamente<br>");
    }
    else
    {
        echo ("Eror cargando base de datos<br>");
    }   
}    
?>

and conectar.php

<?php

    function conectarse()
    {
       if (!($conexion=mysql_connect("localhost","root","")))
       {
          echo "Error conectando a la base de datos<br>";
          exit();
       }

        if (mysql_select_db("db1", $conexion)) 
       {
           echo("<br>Conectado !!!<br>");
       }
       else
       {
            echo("Fallo al conectar<br>");
       }

       return $conexion;
    }

?>

Thank you very much for the help.

You have converted $obra to an array here:

foreach ($xml as $obra)
{
    $obra = (array) $obra;
[...]

so you need to use that variable as an array:

$ninv = $obra['ninventario'] . '/' . $obra['ncatalogo'];

and so on.

我认为您的问题是$obra$obra不是object.its数组

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM