PHP - Error al insertar valores en una tabla al leer registros de un fichero CSV

 
Vista:
sin imagen de perfil
Val: 29
Ha aumentado su posición en 7 puestos en PHP (en relación al último mes)
Gráfica de PHP

Error al insertar valores en una tabla al leer registros de un fichero CSV

Publicado por Patricia (8 intervenciones) el 06/02/2021 14:10:16
Fatal error: Uncaught Error: Call to a member function execute() on bool in C:\xampp\htdocs\Proyecto1\EXAMENES\SEGUNDO CONTROL\pregunta1.php:61 Stack trace: #0 {main} thrown in C:\xampp\htdocs\Proyecto1\EXAMENES\SEGUNDO CONTROL\pregunta1.php on line 61


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport"
              content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <?php
        require ("datos_conexion.php");
 
        $conexion = new mysqli(HOSTNAME, MYSQL_USUARIO, MYSQL_PASSWORD, MYSQL_DB);
 
        $registros = array();
 
        if (($fichero = fopen("Carrera_sol.csv", "r")) !== false) {
 
 
            $nombres_campos = fgetcsv($fichero, 0, ",", "\"", "\"");
 
 
            $num_campos = count($nombres_campos);
 
            while (($datos = fgetcsv($fichero, 0, ",", "\"", "\"")) !== false) {
                // Crea un array asociativo con los nombres y valores de los campos
                for ($icampo = 0; $icampo < $num_campos; $icampo++) {
                    $registro[$nombres_campos[$icampo]] = $datos[$icampo];
                }
                // Añade el registro leido al array de registros
                $registros[] = $registro;
            }
            echo '<br/>';
 
 
            fclose($fichero);
 
 
 
            echo "Leídos " . count($registros) . " registros<br/>";
            for ($i = 0; $i < count($registros); $i++) {
                for ($icampo = 0; $icampo < $num_campos; $icampo++) {
                    echo $nombres_campos[$icampo] . ":" . $registros[$i][$nombres_campos[$icampo]] . "<br/>";
                }
                echo '<br/>';
            }
 
 
 
            try {
 
                $sql = "INSERT INTO CIUDADES VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
 
 
                $query = $conexion->prepare($sql);
 
                foreach ($registros as $registro) {
 
 
                    $query->execute(array(intval($registro["POS"], $registro["M_F"], $registro["POSCAT"], $registro["CATEGORIA"], $registro["SEXO"], $registro["TOT_CATEGORIA"], $registro["NOMBRE"], $registro["TIEMPO"], $registro["MEDIA"], $registro["KM_H"], $registro["CLUB"])));
                }
                echo 'La inserción de datos ha sido un éxito';
            } catch (Exception $e) {
 
                echo 'Error: ' . $e->getMessage();
            }
        }
        ?>
    </body>
</html>



1
2
3
4
5
6
7
8
9
10
11
/*datos_conexion.php*/
 
 
<?php
 
define("MYSQL_USUARIO", "patricia");
define("MYSQL_PASSWORD", "amazonas");
define("HOSTNAME", "localhost");
define("MYSQL_DB", "APP");
 
?>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 29
Ha aumentado su posición en 7 puestos en PHP (en relación al último mes)
Gráfica de PHP

Error al insertar valores en una tabla al leer registros de un fichero CSV

Publicado por Patricia (8 intervenciones) el 06/02/2021 14:51:54
INSERT INTO CARRERAS*
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
Imágen de perfil de info
Val: 60
Ha aumentado su posición en 3 puestos en PHP (en relación al último mes)
Gráfica de PHP

Error al insertar valores en una tabla al leer registros de un fichero CSV

Publicado por info (17 intervenciones) el 06/02/2021 20:24:20
Hola Patricia, creo que el error lo tienes en la función intval() dentro del execute... intval() solo puede recibir un parámetro y tu le estas enviando varios parámetros.
1
intval($variable)
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar