【问题标题】:PHP 7.x SQLITE3 PDO - is the execute() closing the PDO connection?PHP 7.x SQLITE3 PDO - execute() 是否关闭了 PDO 连接?
【发布时间】:2019-07-20 20:25:31
【问题描述】:

我的这段代码在 SQLITE3 中运行很奇怪,因为与 MYSQL 相同的代码运行良好

问题是第 31 行用“ISSUE”注释的行,因为使用 MYSQL/MariaDB 不需要“重新连接”

现在我最好解释一下

如果没有进入 IF 例程,我没有错误

如果处理了 IF 例程,则第 34 行抛出

Uncaught Error: Call to undefined method PDOStatement::prepare()

如果 IF 中的 $PDO-execute(); 正在破坏 PDO 距离

你可能会说,好吧,没问题,现在你已经修好了……是的,但我想知道为什么会这样。

便携性也是一个重点。如果这是 PDO ...除了连接,脚本的其余部分应该可以工作并在各种受支持的 PDO DB 之间移动

谢谢你,如果你能提示是什么原因和它是什么

<?php

// Create or open a database file
$PDO = new PDO('sqlite:myDatabase.sqlite3');


if( isset($_POST['NoteUpdateText'])  && !empty(trim($_POST['NoteUpdateText'])) ){

    //$testo = $_POST['NoteUpdateText'];

    try {

            $PDO = $PDO->prepare('UPDATE ajax SET testo = :testo WHERE id = :id');
            $PDO->bindValue(':testo', $_POST['NoteUpdateText']);
            $PDO->bindValue(':id', 1);
            $PDO->execute();

        // echo a message to say the UPDATE succeeded
        //echo $stmt->rowCount() . " records UPDATED successfully";
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }
}

// In EVERY case, load the actual DB record and return it to javascript

$PDO = new PDO('sqlite:myDatabase.sqlite3');  // --- ISSUE, theoretically this is already opened at line #3 ---

    try {
            $PDO = $PDO->prepare('SELECT testo FROM ajax WHERE id=1 LIMIT 1'); 
            $PDO->execute(); 
            $row = $PDO->fetch();
            //var_dump($row);
            echo $row["testo"];
        }
    catch(PDOException $e)
        {
        echo $sql . "<br>" . $e->getMessage();
        }       

?>

固定代码

<?php

//include 'db-con2.php';
// table: ajax  
// col: testo

// Create or open a database file
$PDO = new PDO('sqlite:myDatabase.sqlite3');

if( isset($_POST['NoteUpdateText'])  && !empty(trim($_POST['NoteUpdateText'])) ){

    //$testo = $_POST['NoteUpdateText'];

    try {

            $statement = $PDO->prepare('UPDATE ajax SET testo = :testo WHERE id = :id');
            $statement->bindValue(':testo', $_POST['NoteUpdateText']);
            $statement->bindValue(':id', 1);
            $statement->execute();

        // echo a message to say the UPDATE succeeded
        //echo $stmt->rowCount() . " records UPDATED successfully";
        }
    catch(PDOException $e)
        {
        echo $sql . "<br> - IF -" . $e->getMessage();
        }
}

// carica da DB in ogni caso per caricare il P col testo realmente in DB
//$PDO = new PDO('sqlite:myDatabase.sqlite3');

    try {
            $statement = $PDO->prepare('SELECT testo FROM ajax WHERE id=1 LIMIT 1'); 
            $statement->execute(); 

            $row = $statement->fetch();
            //var_dump($row);
            echo $row["testo"];
        }
    catch(PDOException $e)
        {
        echo $sql . "<br> - NORMALE - " . $e->getMessage();
        }       

?>

【问题讨论】:

  • $PDO = $PDO-&gt; 你不觉得不ok吗?
  • @u_mulder 嗨,你好意思? :-) 。我的前提是这段代码与 MYSQL 一起工作
  • 我的意思是 $PDO 持有 connection 并且你 用 PDOstatement 对象覆盖它error 会告诉你。
  • 哎呀!你说的对!!让我解决它
  • @u_mulder 我已将第 34,35 行从 $PDO 更改为 $statement 但现在它在第 36 行抛出未捕获错误:调用未定义方法 PDO::fetch()

标签: php pdo sqlite


【解决方案1】:

为什么要覆盖 $PDO 变量?

$pdo = new PDO('sqlite:myDatabase.sqlite3');



if( isset($_POST['NoteUpdateText'])  && !empty(trim($_POST['NoteUpdateText'])) ){

  //$testo = $_POST['NoteUpdateText'];

 try {

   $stmt = $PDO->prepare('UPDATE ajax SET testo = :testo WHERE id = :id');
   if ($stmt->execute(array(':testo'=>$_POST['NoteUpdateText'], ':id' => 1)))
   {

     // echo a message to say the UPDATE succeeded
     //echo $stmt->rowCount() . " records UPDATED successfully";
   } else {
     // There's error processing updates
     // debug
     print_r($stmt->errorInfo());
   }
  } catch(PDOException $e) {
    echo $sql . "<br>" . $e->getMessage();
  }
}

// In EVERY case, load the actual DB record and return it to javascript

// There's no need to redeclare $PDO 
// $PDO = new PDO('sqlite:myDatabase.sqlite3');  // --- ISSUE, theoretically this is already opened at line #3 ---

  try {
    $stmt = $pdo->prepare("SELECT testo FROM ajax WHERE id=1 LIMIT 1"); // line #34
   $stmt->execute(); 
   $row = $stmt->fetch();
   //var_dump($row);
   echo $row["testo"];
 } catch(PDOException $e) {
   echo $sql . "<br>" . $e->getMessage();
 }

【讨论】:

  • 您好,谢谢,这是比较技术性的,但由于我正在学习,这是下一步,您能帮忙提供原始代码吗?
猜你喜欢
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
  • 2015-06-25
相关资源
最近更新 更多