PHP 7.x SQLITE3 PDO-execute()是否关闭PDO连接?

时间:2019-02-27 07:15:30

标签: php pdo sqlite

我有这段代码对SQLITE3很奇怪,因为与MYSQL相同的代码也能正常工作

问题是该行在第31行带有“问题”的注释行,因为对于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();
        }       

?>

1 个答案:

答案 0 :(得分:0)

为什么要覆盖$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();
 }