如何使用php更新Oracle数据库中的Blob图像?

时间:2019-01-14 05:08:09

标签: php oracle oracle11g blob

我想使用PHP更新数据库中的image(blob)。我能够成功插入它,但无法更新它。 我收到“指定了无效的LOB定位符”错误

这是插入查询:

$ myblobid = 157;

  // Insert the BLOB from PHP's tempory upload area

  $lob = oci_new_descriptor($db, OCI_D_LOB);
  $stmt = oci_parse($db, 'INSERT INTO JOB_SEEKER (SEEK_ID, SEEK_PICTURE) '
         .'VALUES(:MYBLOBID, EMPTY_BLOB()) RETURNING SEEK_PICTURE INTO :BLOBDATA');
  oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
  oci_bind_by_name($stmt, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
  oci_execute($stmt, OCI_DEFAULT);

  // The function $lob->savefile(...) reads from the uploaded file.
  // If the data was already in a PHP variable $myv, the
  // $lob->save($myv) function could be used instead.
  if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
    oci_commit($db);
  }
  else {
    echo "Couldn't upload Blob\n";
  }
  $lob->free();
  oci_free_statement($stmt);

我尝试了更新查询:

 $myblobid = 157;
      // Update 
      $lob = oci_new_descriptor($db, OCI_D_LOB);
      $stmt = oci_parse($db, 'UPDATE JOB_SEEKER SET SEEK_PICTURE = :BLOBDATA WHERE SEEK_ID = :MYBLOBID ');
      oci_bind_by_name($stmt, ':MYBLOBID', $myblobid);
      oci_bind_by_name($stmt, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
      oci_execute($stmt, OCI_DEFAULT);

      // The function $lob->savefile(...) reads from the uploaded file.
      // If the data was already in a PHP variable $myv, the
      // $lob->save($myv) function could be used instead.
      if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
        oci_commit($db);
      }
      else {
        echo "Couldn't upload Blob\n";
      }
      $lob->free();
      oci_free_statement($stmt);

1 个答案:

答案 0 :(得分:0)

这解决了我的问题:

 $lob = oci_new_descriptor($db, OCI_D_LOB);
   $myblobid = 157;
  $stmt = oci_parse($db, "UPDATE JOB_SEEKER SET SEEK_PICTURE = EMPTY_BLOB() WHERE SEEK_ID = '$myblobid' RETURNING SEEK_PICTURE INTO :BLOBDATA");

  oci_bind_by_name($stmt, ':BLOBDATA', $lob, -1, OCI_B_BLOB);
  oci_execute($stmt, OCI_DEFAULT);

  // The function $lob->savefile(...) reads from the uploaded file.
  // If the data was already in a PHP variable $myv, the
  // $lob->save($myv) function could be used instead.
  if ($lob->savefile($_FILES['lob_upload']['tmp_name'])) {
    oci_commit($db);
  }
  else {
    echo "Couldn't upload Blob\n";
  }
  $lob->free();
  oci_free_statement($stmt);