Save an array to Mysql

Asked

Viewed 511 times

0

I’m trying to record some information on Mysql through a Array obitda of an external execution, below the code I am using:

$licencas = exec(escapeshellcmd($comando), $output);
reset($output);
while (list(,$line) = each($output)){ 
echo $line, "<BR>\n"; 
$sql= mysqli_query ($conexao,"INSERT INTO licenca (cnpj,dados, data) VALUES ('$cnpj', '$line', '$data')");
}

It is working, but when it stores in the table, it stops recording some lines that were obtained, in echo appears all lines as I need.

Who can help me.

Thank you very much.

  • Insert a mysqli_error and see if anything returns. $sql= mysqli_query ($conexao,"INSERT INTO licenca (cnpj,dados, data) VALUES ('$cnpj', '$line', '$data')") or die(mysqli_error($conexao);

  • I added, presented this message! 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’s : 1', '2019-02-08 08:24')' at line 1

  • Print the query you are running, the message says you have some syntax error

  • Hi Pedro, I searched here and I managed to solve it this way: $licencas = exec(escapeshellcmd($command), $output); reset($output); while (list(,$line) = each($output)){ &#xA;echo $line, "<BR>\n"; $stmt = mysqli_prepare($conexao, "INSERT INTO licenca (cnpj,dados, data) VALUES(?,?,?)");&#xA;mysqli_stmt_bind_param($stmt, 'sss', $cnpj, $line, $data);&#xA;mysqli_stmt_execute($stmt);&#xA;} Será assim o melhor jeito?

2 answers

1

You can simply use PHP Serialization according to the documentation itself. https://www.php.net/manual/en/function.serialize.php

When reusing you can use Unserialization to return the array to its "real format".

$serializedArray = serialize($array);
$unserializedArray = unserialize($array);

Your code would look something like this:

$licencas = exec(escapeshellcmd($comando), $output);
reset($output);
while (list(,$line) = each($output)){ 
echo $line, "<BR>\n"; 
$sql= mysqli_query ($conexao,"INSERT INTO licenca (cnpj,dados, data) VALUES ('$cnpj', '$line', '".serialize($data)."')");
}

1

I decided as follows:

//Executa o comando para verificar licença e armazena na variavel $line
$licencas = exec(escapeshellcmd($comando), $output);

reset($output);
while (list(,$line) = each($output)){ 

//Grava resultado da execução da execução no banco de dados
$stmt = mysqli_prepare($conexao, "INSERT INTO licenca (cnpj,dados, data) VALUES(?,?,?)");

mysqli_stmt_bind_param($stmt, 'sss', $cnpj, $line, $data);

mysqli_stmt_execute($stmt);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.