I tell you my problem:
I have a csv file with a certain amount of data, what I do is open this file and modify some fields that are badly loaded (Phones with spaces, hyphens, etc ...) and then I have to load them in my database. Postgress data:
#!/usr/bin/php
<?php
// Me conecto al pgsql
$conectarse = pg_connect(******);
// Reviso si la conexion fue exitosa sino devuelvo un error
if (!$conectarse) {die("Error de Conexion!: " . pg_last_error());}
// --- FUNCIONES --- ///
// Reemplaza los guiones bajo por espacios.
function sacar_guiones($var){
$reemplazo=str_replace("_", " ", $var);
return $reemplazo;
}
// Toma todo el string y solo deja los numeros y en caso de que no devuelva nada (devuelve 0 si no hay numeros) lo reemplaza por nada.
function telefono($numero) {
$r = intval(preg_replace('/[^0-9]+/', '', $numero), 10);
if($r == 0){$r = "";}
return $r;
}
// --- FIN FUNCIONES --- ///
// --- PROGRAMA PRINCIPAL --- ///
// Ignora las advertencias de PHP
error_reporting(0);
// Abro el archivo CSV en modo lectura
$file = fopen('datos-ejemplo.csv', 'r');
// Creo una bandera que utilizo para ignorar la primer vuelta asi no me guarda los cabezales
$band = 0;
// Recorro el archivo
while (($line = fgetcsv($file)) !== FALSE) {
// Consulto si es el primer ingreso, para evitar leer las cabeceras, si lo es cambio la bandera a 1
if($band != 0 )
{
// Almaceno los datos recolectados
$RAZON=$line[14];
$LOCALIDAD=$line[16];
$EMAIL=$line[15];
$FECHADEREGISTRO=$line[1];
$TELEFONO1=telefono($line[12]);
$TELEFONO2=telefono($line[13]);
// Genero la consulta SQL
$query = "INSERT INTO tabla_datos (razon) VALUES ('" .$RAZON. "')";
// Ejecuto la consulta
$result = pg_query_params($conectarse, $query);
// Verifico si la consulta se llevo a cabo correctamente, sino aviso
if (!$result) die("\nError in SQL query: " . pg_last_error() . "\n");
}
else
$band = 1;
// fin del if
} // end-while
// Cierro el archivo
fclose($file);
// Termino la conexion
pg_close($conectarse);
?>
The connection is made correctly, but at the time of making an insert it gives me the following error: "Error in SQL query:" that is, the error message that I have in case the query fails but does not give me information about because it fails
Does anyone know what may be wrong? I was all day trying to make him walk and I could not = (
Thank you!
EDIT: If I try to do the insert with int if it is taken, the problem is in the date and the string