在数组中找不到方法'fetch_assoc'-错误我该如何解决?

时间:2019-04-09 16:16:37

标签: php mysql

我正在尝试从数据库中检索信息并将其放入PHP和HTML表格中,但是,我遇到了一个问题,该问题表明ive出现了错误:

  

致命错误:未捕获错误:函数名称必须为字符串   F:\ Client Server \ Website \ campsites.php:41堆栈跟踪:#0 {main}   抛出

当我检查错误所在的行时说:

  

在数组中找不到方法'fetch_assoc'

我该如何解决?

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $search = $_POST['search'];

    $_SESSION['search'] = $search;

    $sqlQuery = "SELECT * FROM campsites ='$search'";
    $result = $campDataSet->fetchAllCamps($sqlQuery);

    if($result > 0){

        echo'<div class="table-responsive">
                <table class="table">
                    <thead id="table1Head">
                    <tr><td>campID</td>
                        <td>Name</td>
                        <td>Address</td>
                        <td>Postcode</td>
                        <td>Country</td>
                        <td>Latitude</td>
                        <td>Longitude</td>
                        <td>email</td>
                        <td>Phone<td></tr>
                    </thead>
                    <tbody>

              </div>';

        while($row = $result->fetch_assoc()){
            echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>';
        }
        echo "</tbody></table>";
    } else {
        print " 0 results";
    }
}

编辑:

var_dump($result);

array(1) { [0]=> object(DBdata)#6 (11) { ["db"]=> NULL ["conn"]=> object(PDO)#7 (0) { } 
    ["id_campsite"]=> string(1) "1" ["campsite_name"]=> string(13) "lake district" ["address"]=> string(32) "lake distrcit greater manchester"  
    ["postcode"]=> string(7) "m21 8fx" ["country"]=> string(7) "England" ["lattitude"]=> string(13) "1 0800 243230" ["longitude"]=> string(12) "120312310978" 
     ["email"]=> string(14) "lake@gmail.com" ["phone_number"]=> string(12) "059874327823" } }

2 个答案:

答案 0 :(得分:1)

假设fetchAllCamps()为您执行了提取操作,这就是为什么要使用数组而不是结果集的原因,所以要使用行而不是行...

while($row = $result->fetch_assoc()){

替换
foreach ( $result as $row ) {

答案 1 :(得分:1)

此错误的一个可能原因是mainoop()返回一个数组。在这种情况下,您的代码应如下所示:

$campDataSet->fetchAllCamps($sqlQuery)

更新:

您的<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') { $search = $_POST['search']; $_SESSION['search'] = $search; $sqlQuery = "SELECT * FROM campsites ='$search'"; $result = $campDataSet->fetchAllCamps($sqlQuery); if (count($result) > 0) { echo'<div class="table-responsive"> <table class="table"> <thead id="table1Head"> <tr><td>campID</td> <td>Name</td> <td>Address</td> <td>Postcode</td> <td>Country</td> <td>Latitude</td> <td>Longitude</td> <td>email</td> <td>Phone<td></tr> </thead> <tbody> </div>'; foreach ($result as $row) { echo '<td>' . $row['id_campsite']. '</td> <td>' . $row['campsite_name'] . '</td> <td>' . $row['address'] . '</td> <td>' . $row['postcode']. '</td> <td>' . $row['country']. '</td> <td>' . $row['lattitude']. '</td> <td>' . $row['longitude']. '</td> <td>' . $row['email']. '</td> <td>' . $row['phone_number']. '</td> </td>'; } echo "</tbody></table>"; } else { print " 0 results"; } } ?> 返回一个对象数组。在这种情况下,这更合适:

$campDataSet->fetchAllCamps