Error when inserting an image in Oracle using PHP PDO

0

I am making a system in PDO PHP which writes some images to the database, that same system was migrated from a MySQL database and was working perfectly, however at the time I'm going to record the image in a column of type BLOB in Oracle returns the following exception:

  

[Thu Mar 02 13: 11: 37.290759 2017] [: error] [pid 5740: tid 2192] [client   :: 1: 54413] PHP Fatal error: Uncaught exception 'PDOException' with   message 'SQLSTATE [HY000]: General error: 972 OCIStmtPrepare:    ORA-00972: long identifier \ n (ext \ pdo_oci \ oci_driver.c: 339) 'in   C: \ xampp \ htdocs \ MaintenanceOracle \ administrator.php: 1581 \ nStack   trace: \ n # 0   C: \ xampp \ htdocs \ MaintenanceOracle \ administrator.php (1581):   PDO-> exec ('insert into equ ...') \ n # 1 {main} \ n thrown in   C: \ xampp \ htdocs \ MaintenanceOracle \ administrator.php on line 1581,   referer: link

Follow the insert in the database:

  

$query = $con->exec("insert into equipamentos (id,grupo,quantidade,descricao,marca_modelo,patrimonio,serie,tipo_ponto,foto_acabamento,foto_maquina) values (".$maior_id.",'".$_POST['labelGrupo']."',".$_POST['labelQuantidades'].",'".$_POST['labelDescricao']."','".$_POST['labelMarcaModelo']."',".$_POST['labelPatrimonio'].",'".$_POST['labelSerie']."','".$foto_ponto_certa."','".$foto_acabamentos_certa."','".$foto_maquina_certa."')");

Create from table:

  

CREATE TABLE equipamentos (id INTEGER NOT NULL,grupo VARCHAR2(50),quantidade INTEGER,descricao VARCHAR2(50),marca_modelo VARCHAR2(50),patrimonio INTEGER,serie VARCHAR2(50), tipo_ponto BLOB, foto_acabamento BLOB, foto_maquina BLOB,PRIMARY KEY (id))

Treatment of php images before insertion into the database:

// ARMAZENA A PRIMEIRA FOTO NOS ARRAYS
$foto_ponto = $_FILES['fotoPonto']['tmp_name'];
$foto_ponto_tamanho = $_FILES['fotoPonto']['size'];
$foto_ponto_tipo = $_FILES['fotoPonto']['type'];
$foto_ponto_nome = $_FILES['fotoPonto']['name'];


// TRATA A PRIMEIRA FOTO PARA FAZER A INSERÇÃO DELA NO BANCO DE DADOS
$fp = @fopen($foto_ponto, "rb");
$conteudo = @fread($fp,$foto_ponto_tamanho);
$foto_ponto_certa = @addslashes($conteudo);
@fclose($fp);

Remarks:

  • I have only treated the first picture so it is not too long, the treatment of all the photos is the same.
  • This same code works perfectly in a MySQL database.
asked by anonymous 02.03.2017 / 18:13

1 answer

0

Hello, I suggest you refactor a little more, since you have switched from MySQL to Oracle and you are entering a record with LOB field. Here is a didactic suggestion:

$conn = ocilogon('usuario','senha','nome_da_base');

$arquivo  = "Open Office.zip";
$handle   = fopen($arquivo, "rb");
$conteudo = fread($handle, filesize($arquivo));

$stmt = oci_parse($conn, "INSERT INTO TABELA_BLOB VALUES (EMPTY_BLOB()) RETURNING ANEXO INTO :ANEXO");
$blob_aux = oci_new_descriptor($conn, OCI_D_LOB);
oci_bind_by_name($stmt, ":ANEXO", &$blob_aux, -1, OCI_B_BLOB);
oci_execute($stmt, OCI_DEFAULT);
$blob_aux->save($conteudo);

if (ocicommit($conn)) {
   echo "Registro inserido OK.";
} else {
   echo "ERRO, registro não inserido.";
}
    
02.03.2017 / 20:42