簡體   English   中英

“ while($ row = mysqli_fetch_assoc($ result)){//}”有什么問題?

[英]What is the wrong with “while($row = mysqli_fetch_assoc($result)){// }”?

我嘗試了幾種方式的以下代碼。 但是問題總是出現在"$row = mysqli_fetch_assoc($result)" ..... print_r()方法將結果打印為數組,這意味着MYSQL查詢沒有錯誤。 它給出正確的結果。 問題出在assoc中。 而不是'assoc'我也使用了'array'

錯誤說:“警告:mysqli_fetch_assoc()期望參數1為mysqli_result,第21行的C:\\ xampp \\ htdocs \\ offoptimizer2 \\ action \\ fetch_client.php中給出的數組”

class fetch_client{
        public function fetchdata(){
                $phn_number = $_GET["phn"];

                $db_action = new db_action();
                $result = $db_action->select_all("offopt_client","client_phn_no='".$phn_number."'");
                print_r($result);

                echo '<table border="0">';
                echo '<tr>';
                    echo '<th> client name </th>';
                    echo '<th> client phone number </th>';
                echo '</tr>';
                if($result != null){
                        while($row = mysqli_fetch_assoc($result)){
                            echo '<tr>';
                                echo "<td> ".$row['client_name']." </td>";
                                echo "<td> ".$row['client_phn_no']." </td>";
                            echo '</tr>';   
                            }
                        echo '</table>';
                    }else{
                            echo 'No result';
                        }


            }
    }

//這是我下面的select_all函數

public function select_all($table_name, $where){
                $db_connect = new db_connect();
                $con = $db_connect->connect();
                if(!$con){
                        echo "Connection error";
                    }else{
                            $query = mysqli_query($con,"select * from ".$table_name." where ".$where." ");
                            if(!$query){
                                    echo "Error in your query".mysqli_error($query);
                                }else{
                                        $result = mysqli_fetch_array($query);
                                        return $result;
                                    }
                        }
            }

如下更新您的“ select_all”功能。

public function select_all($table_name, $where){
                $db_connect = new db_connect();
                $con = $db_connect->connect();
                if(!$con){
                        echo "Connection error";
                    }else{
                            $query = mysqli_query($con,"select * from ".$table_name." where ".$where." ");
                            if(!$query){
                                    echo "Error in your query".mysqli_error($query);
                                }else{
                                        $result = array();
                                        while($row = mysqli_fetch_assoc($query))
                                        array_push($result, $row);
                                        return $result;
                                    }
                        }
            }

現在,您可以對以下結果使用foreach循環:

foreach($result as $row)
   echo '<tr>';
   echo "<td> ".$row['client_name']." </td>";
   echo "<td> ".$row['client_phn_no']." </td>";
   echo '</tr>';   
}

mysqli_fetch_assoc需要一個ressource而不是一個array 因此,這看起來像您的$db_action->select_all方法將您的數據庫數據返回到數組中一樣,因此對mysqli函數執行附加任務並產生結果是行不通的。

如果您希望獲取assoc返回數據,請在$db_action之后編輯您的類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM