2017-01-14 8 views
0

以下のコードでは、実行中に警告が生成されます。警告:mysqli_fetch_assoc()は、パラメータ1がmysqli_resultになると予想しています。これを解決する方法は?この警告を解決するには:mysqli_fetch_assoc()は、パラメータ1がmysqli_resultになることを期待します。

<?php 
    require '../smarty3/libs/Smarty.class.php'; 
    $smarty = new Smarty; 
    class conn 
    { 
       public $conn = ''; 
       public function fetchAll() 
       { 
        $sql = "SELECT * FROM test" ; 
        if(mysqli_query($this->conn,$sql)) 
        { return 'success'; } 
       else{ return 'error'; } 
       } 
    } 
    $db = new conn(); 
    $record = $db->fetchAll(); 
    if($record != 'error'){ 
     while($row = mysqli_fetch_assoc($record)) 
     { 
      header('location : view.tpl'); 
     } 
    }else{ echo "No Records Found";} 
    $smarty->display('view.tpl'); 
    ?> 
    **View.tpl** 
    <table align = "center"> 
     {section name = i loop = $row} 
      <tr> <td> {$smarty.section.i.rownum} </td> 
       <td> {$row[i].name} </td> 
       <td> {$row[i].country} </td> 
      </tr> 
     {/section} 
    </table> 

ありがとうございました。

答えて

0

$record = $db->fetchAll()はmysqlリソースではなく、文字列(successまたはerror)を返しているからです。今fetchAll()方法が成功したクエリにmysqli_resultを返します

class conn 
     { 
        public $conn = ''; 
        public function fetchAll() 
        { 
         $sql = "SELECT * FROM test" ; 
         $r = mysqli_query($this->conn,$sql) 
         if($r) 
         { 
         return $r; } 
        else{ return 'error'; } 
        } 
     } 

、または'error'私の意見では

ない場合、これはするための最良の方法ではありません。

にコネティカットクラスを変更することを検討してくださいエラーを処理します。 try/catchブロックを調べ、クラスからスタックまでのエラーを "スロー"する方法を検討してください。

+0

その作業。ありがとう – himani

関連する問題