bonjour
j'ai retouche un code pour inserer un .csv dans du sql mais quand je tente de les rentrer dans la bdd j'ai des erreurs incomprehensibles
par contre lorsque je désactive les modules de la base de données , j'ai un retour de tableau correct
voici le code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 array(4) { [0]=> array(3) { [0]=> string(6) "ggg" [1]=> string(3) "ttt" [2]=> string(2) "54" } [1]=> array(3) { [0]=> string(4) "bbbb" [1]=> string(3) "fff" [2]=> string(2) "21" } [2]=> array(3) { [0]=> string(3) "ggg" [1]=> string(3) "kkk" [2]=> string(2) "78" } [3]=> array(3) { [0]=> string(3) "jjj" [1]=> string(3) "sss" [2]=> string(2) "44" } }
j'ai essayé par le module d'enregistrement #1 mais j'ai cette erreur :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 <?php $filename = 'amazon-fours.csv'; // The nested array to hold all the arrays $the_big_array = []; // Open the file for reading if (($h = fopen("{$filename}", "r")) !== FALSE) { // Each line in the file is converted into an individual array that we call $data // The items of the array are comma separated while (($data = fgetcsv($h, 0, ";")) !== FALSE) { // Each individual array is being pushed into the nested array $the_big_array[] = $data; } // Close the file fclose($h); } include("db_connect.php"); // module d'enregistrement #1 mysqli_query($conn,'LOAD DATA LOCAL INFILE \'amazon-fours.csv\' INTO TABLE produits FIELDS TERMINATED BY \';\' ENCLOSED BY \'"\' LINES TERMINATED BY \'\n\' ') or die("Erreur mise à jour table : ".mysqli_error($conn)); /* // module d'enregistrement #2 $data = [ 'name' => $the_big_array[1], 'description' => $the_big_array[2], 'prix' => $the_big_array[3] ]; $sql = "INSERT INTO file VALUES (NULL,:name,:description,:prix)"; $stmt= $conn->prepare($sql); $stmt->execute($data); // afficher le code echo "<pre>"; var_dump($the_big_array); echo "</pre>"; */ ?> db_connect.php <?php $server = "localhost"; $username = "*******"; $password = "*******"; $db = "*********"; $conn = mysqli_connect($server, $username, $password, $db); session_start(); $_SESSION[`connect`] = $conn ; ?>
j'ai donc essayé la module d'enregistrement #2Erreur mise à jour table : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '$the_big_array[] INTO TABLE produits FIELDS TERMINATED BY ';' ENCLOSED BY...' at line 1
mais la aussi j'ai un message d'erreur :
nb : j'ai verifié db_connect la connection est bonne j'ai bien le echo en retourWarning: Trying to access array offset on value of type bool in /home/u320889525/domains/best-products.fr/public_html/importcsv.php on line 34
Warning: Trying to access array offset on value of type bool in /home/u320889525/domains/best-products.fr/public_html/importcsv.php on line 35
Warning: Trying to access array offset on value of type bool in /home/u320889525/domains/best-products.fr/public_html/importcsv.php on line 36
Fatal error: Uncaught Error: Call to a member function execute() on bool in /home/u320889525/domains/best-products.fr/public_html/importcsv.php:40 Stack trace: #0 {main} thrown in /home/u320889525/domains/best-products.fr/public_html/importcsv.php on line 40
version php ; 8.0
Partager