2017-02-13 5 views
1

クラス内の関数を介してMySqlに接続しようとしています。同じことがより簡単に実行できますが、これは学習の経験でもあります。 mysqli_error()は空白を返し、mysqli_errno()は0を返します。エラーを手動でmysqlに入力しましたが、dbのカラムが短すぎます。 mysqli_error()やmysql_errno()でエラーが報告されない理由を理解できません。助けを前にありがとう。PHPのレポートからのmysqlエラー報告0

<?php 

class FrmAddProduct { 

    protected function getDbConnection(){ 
     return new mysqli("localhost", "root", "****", "test"); 
    } 

    /** 
    *all variables are declares and assigned here 
    * 
    **/ 
    function commitSignUp(){ 
     $commitSignUp = "INSERT INTO Login (`logTitle`,`logFirstName`, `logLastName`,`logLandLine`,) VALUE (\"$this->title\", \"$this->firstName\",\"$this->lastName\", \"$this->landLine\",)"; 
     if ($this->getDbConnection()->query($commitSignUp)){ 
      echo "The new product is added."; 
      return 1; 
     } else { 
      echo "Mysql reported this error description: ".mysqli_error($this->getDbConnection()."<br>"; 
       echo "Mysql reported this error number: ".mysqli_errno($this->getDbConnection()); 
       return 0; 
     } 
+0

あなたはMySQLiをOOPスタイルと手続きスタイルを混合している...どちらか一方を使用してこれを行うには、混在させることはできません。マニュアルの詳細:http://php.net/manual/en/mysqli.error.php –

答えて

1

ここで行っていることはまったく間違ったアプローチです。接続のインスタンスを2回または3回作成しています。そしてあなたはそのエラーがそこに書き込まれることを期待しています。

このようにコードを変更してください。

protected function getDbConnection(){ 
    return mysqli_connect("localhost", "root", "****", "test"); 
} 

、あまりにも

function commitSignUp(){ 
     $commitSignUp = "INSERT INTO 
          Login (
            `logTitle`, 
            `logFirstName`, 
            `logLastName`, 
            `logLandLine`, 
            ) 
          VALUE (
             \"$this->title\", 
             \"$this->firstName\", 
             \"$this->lastName\", 
             \"$this->landLine\", 
         )"; 

      $conn = $this->getDbConnection(); 

      if ($conn->query($commitSignUp)) { 
       echo "The new product is added."; 
       return 1; 
      } else { 
       echo "Mysql reported this error description: ".mysqli_error($conn."<br>"; 
       echo "Mysql reported this error number: ".mysqli_errno($conn); 
       return 0; 
      } 
+0

これは主要な問題から何も変わりません。 OPの現在のコードと同じように、同じことをします。 –

+0

試してみましたか? – Vinay

+0

マルチ接続での唯一の問題です。 – Vinay

関連する問題