2

Estoy intentando almacenar estos datos JSON:

{
    "ticker": {
        "base": "ETH",
        "target": "USD",
        "price": "689.18009232",
        "volume": "234831.85174911",
        "change": "1.48138601"
    },
    "timestamp": 1525308422,
    "success": true,
    "error": ""
}

En mi Base de Datos y al ejecutar el PHP me salen estos errores:

PHP Notice: Undefined index: ticker in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Undefined index: ticker in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Notice: Undefined index: ticker in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Undefined index: ticker in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Warning: Illegal string offset 'ticker' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Warning: Illegal string offset 'ticker' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Warning: Illegal string offset 'base' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Warning: Illegal string offset 'base' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Warning: Illegal string offset 'ticker' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Warning: Illegal string offset 'ticker' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Warning: Illegal string offset 'volume' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Warning: Illegal string offset 'volume' in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23 PHP Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

Notice: Uninitialized string offset: 0 in C:\xampp\htdocs\crypto4allfinal\jsonToMySQL.php on line 23

A continuación dejo el código PHP:

<?php

/*$bucle=true;
do {*/

    $json = file_get_contents('datosCriptomonedas.json');
    $datosclientes = json_decode($json, true);

    $server = "localhost";
    $user = "root";
    $pass = "";
    $bd = "crypto4all";

    //Creamos la conexión
    $conexion = mysqli_connect($server, $user, $pass,$bd) 
    or die("Ha sucedido un error inexperado en la conexion de la base de datos");

    //var_dump($cliente);

    foreach ($datosclientes as $cliente) {

        mysqli_query($conexion,"INSERT INTO criptomoneda (nombre,volumen) 
        VALUES ('".$cliente['ticker']['base']."',".$cliente['ticker']['volume'].")");
    }   

    mysqli_close($conexion);
/*
    sleep(60);//1 minuto
} while ($bucle);*/

?>

El resultado de la variable es el siguiente:

Array
(
    [ticker] => Array
        (
            [base] => ETH
            [target] => USD
            [price] => 689.18009232
            [volume] => 234831.85174911
            [change] => 1.48138601
        )

    [timestamp] => 1525308422
    [success] => 1
    [error] => 
)
7
  • No veo el código
    – Raul A.
    el 3 may. 2018 a las 15:19
  • @RaulA. He quitado las etiquetas de php ya.
    – charli
    el 3 may. 2018 a las 15:27
  • Hola, podrías hacer print_r($datosclientes) para ver como se están guardando los datos en esa variable. el 3 may. 2018 a las 15:38
  • 1
    El problema es que estás iterando sobre el mapa, los índices en tu foreach van a ser ticker, timestamp, success, error. Por eso te sale el error de índice inexistente
    – user22721
    el 3 may. 2018 a las 15:47
  • 2
    Por eso, debes acceder directamente a $datosclientes y no utilizar el foreach. Así: $datosclientes['ticker']['base']
    – user22721
    el 3 may. 2018 a las 15:48

1 respuesta 1

3

En este bucle tu estás iterando sobre los índices de $datoscliente

foreach ($datosclientes as $cliente) {

    mysqli_query($conexion,"INSERT INTO criptomoneda (nombre,volumen) 
    VALUES ('".$cliente['ticker']['base']."',".$cliente['ticker']['volume'].")");
}

Lo que debes hacer es acceder directamente al arreglo original y no utilizar el foreach

mysqli_query($conexion,"INSERT INTO criptomoneda (nombre,volumen) 
VALUES ('".$datoscliente['ticker']['base']."',".$datoscliente['ticker']['volume'].")");

Por cierto estás expuesto a un grave problema de seguridad llamado Inyección SQL, puedes ver aquí como evitarla: ¿Cómo evitar la inyección SQL en PHP?

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.