0

私はPDOの初心者です。1 つのプロジェクトで複数のデータベースを使用する方がプログラマーにとって使いやすく、安全性が高いと聞いたので、PDOを学びたいと考えています。PDOを使用してMySQLにデータを挿入したいのですが、できません。エラー メッセージも表示されません。次のコードを使用しました。

<?php 

   class ManageUsers02 {             
     private   $db_con;
     public    $db_host  = "localhost";  // I run this code on my localhost
     public    $db_name  = "todo";       // database name
     public    $db_user  = "root";       // username
     public    $db_pass  = "";           // password

     function __construct() {           
        try {
            // connect to database using PDO
           $this->db_con = new PDO("mysql:host=$this->db_host;db_name=$this->db_name", $this->db_user, $this->db_pass);             
        } catch (PDOException $exc) {  // PDO exception handling
           echo $exc->getMessage();    
        }             
     }

     public function reg_user($username, $password, $ip_address, $reg_date, $reg_time ) {
       try {
          // preparing sql query 
          $query = $this->db_con->prepare("INSERT INTO user_reg (username, password, ip_address, reg_date, reg_time) VALUES ( ?, ?, ?, ?, ? )");  
       }
       catch( PDOException $exc )  {  // PDO exception handling
          echo $exc->getMessage();
       }

       try {
           // execute sql query
           $query->execute(array($username, $password, $ip_address, $reg_date, $reg_time)); 
       }
       catch( PDOException $exc )  {
          echo $exc->getMessage();
       }

       $counts = $query->rowCount();  // return value of affected row
       // here it should be return 1       

       echo "<br /> count :: <b> " . $counts . "</b> <br />";  // shows result 0
       // no value inserted 
     }


   }

   $user_reg = new ManageUsers02();         

   $user_reg->reg_user('pdo_name', 'pdo_password', '127.0.0.1', '2013-2-6', '4:20 am');
 ?>
4

3 に答える 3

0

これは、文字列を mysql の Time フィールドに挿入するために失敗します。

$sql =  "INSERT INTO user_reg ( ... , reg_time) VALUES (..., '4:20 am');

使いたい'4:20 am'なら使えばいい。

TIME( STR_TO_DATE( ? , '%h:%i %p' ))

お気に入り

$sql =  "INSERT INTO user_reg ( ... , reg_time) VALUES 
                ( ?, ?, ?, ?, TIME( STR_TO_DATE( ? , '%h:%i %p' )))";

クラスに aokと aを与えcountsます。

<?php 

   class ManageUsers02 {             
     ...
     public    $counts   = 0;
     public    $ok       = false;

     function __construct() {           
        try {
          $this->db_con = new PDO("mysql:dbname=$this->db_name;host=$this->db_host", $this->db_user, $this->db_pass);             
        } catch (PDOException $exc) {  // PDO exception handling
           echo $exc->getMessage();
          return;
        } 
        if (!$this->db_con) {
          return;
        }
     $this->ok = true;   
     }

     public function reg_user($username, $password, $ip_address, $reg_date, $reg_time ) {
       $this->counts = 0;
       $this->ok = false;     
       $sql =  "INSERT INTO user_reg (username, password, ip_address,
                reg_date, reg_time) VALUES 
                ( ?, ?, ?, ?, TIME( STR_TO_DATE( ? , '%h:%i %p' )))";
       try {              
        $query = $this->db_con->prepare($sql);  
       }
       catch( PDOException $exc )  {  // PDO exception handling
        echo $exc->getMessage();
        return;
       }
        if (!$query) {
          return;
        }
       try {
       $this->ok = $query->execute(array($username, $password, $ip_address, $reg_date, $reg_time));
       }
       catch( PDOException $exc )  {
          echo $exc->getMessage();
          $this->ok = false;
          return;
       }
       if ($this->ok) {
       $this->counts = $query->rowCount();  // return value of affected row
       }
     }
   }

   $user_reg = new ManageUsers02();
   if ($user_reg->ok) {
       $user_reg->reg_user('pdo_name4', 'pdo_password4',
       '127.0.0.1', '2013-2-6', '04:20 am' );
       if ($user_reg->ok) {
              echo "<br /> count :: <b> " . $user_reg->counts . "</b> <br />";
   } else { echo "Error : Insert failed";}
   } else { echo "Error : Connection failed: ";}
 ?>
于 2013-09-08T05:53:40.750 に答える