donglieshe4692 2019-02-27 07:15
浏览 324

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

I have this code that works weird with SQLITE3 , since the same code with MYSQL works fine

The issue is the line commented with "ISSUE" at line #31, because with MYSQL/MariaDB that "re connection" is NOT needed

Now I better explain

If the IF routine is not entered, I have NO error

If the IF routine is processed, line #34 throws

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

like if the $PDO-execute(); inside the IF is destroying the PDO istance

You may say, well, no problem, now you have fixed it ... yes, but I'd like to understand why this happen.

Also portability is a point. If this is PDO ... except for the connection, the rest of the script should work and moved among various supported PDO DBs

Thank you if you kindly hint what is the reason and what is it

<?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();
        }       

?>

FIXED CODE

<?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条回答 默认 最新

  • douxianwu2221 2019-02-27 07:24
    关注

    Why would you override $PDO variable ?

    $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();
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型