php - mysqli_fetch_assoc 返回为 foreach() 提供的参数无效

标签 php mysql foreach

我有一个到数据库的连接,我正在使用mysqli_fetch_assoc创建一个关联数组以将数据返回到屏幕。这部分代码如下所示:

    $sql = "SELECT * FROM Kundregistert ORDER BY ID DESC;";
    //
    //
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    $row = mysqli_fetch_assoc($result);
    //
    // Avsluta anslutning till db
    return ($row['Namn'], $row['Telefonnummer'], $row['Epost'], $row['Dacktyp'], $row['Lagerstatus'], $row['Datum']);
    mysqli_close($conn);
}

起初,我将数据库中的所有信息存储在名为“Kund”的一列下(我只使用了return $row['Kund'],因为它只有一列),那么它就不会提供 foreach 错误。现在我通过 7 个变量存储信息,并希望返回所有列,就像我上面尝试做的那样。

这是 foreach 的代码;

foreach($company->getCustomerList() as $key => $obj){
    echo "<h3 id='id'>$key</h3> " . $obj->getcustomername() . ", " . $obj->getcustomertfn() . ", " . $obj->getcustomerepost() . "<br>" . $obj->gettirebrand() . ", " . $obj->getstatus() . ": " . $obj->getdate() . "<br>" . 
    " <a href='classes_serialisering.php?delPart=$key' id='radera'>Radera post $key </a>" . " <a href='classes_serialisering.php?updateView=$key' id='uppdatera'>Uppdatera Post </a><br>";
}

起初,当我只有一列时,我尝试将 return $row[''] 更改为“Kund”以外的其他内容,然后我还得到了 foreach错误。所以我认为我尝试返回列的方式有问题,但我似乎无法使其工作。

我是 php 的初学者,所以请记住这一点。

谢谢。

编辑*****

interface RegisterCustomer {
function addCustomer($customername, $customertfn, $customerepost, $tirebrand, $status);
function getCustomerList();
}

class Company implements RegisterCustomer { 
protected $companyname = '';
public $CustomerList = array();

function read_data(){
    //
    //Anslut mot databas
    $conn = mysqli_connect('xxxxx', 'xxxxx', 'xxxxx')
    or die('Could not establish connection to MySQL.');
    $db_connected = mysqli_select_db($conn, "xxxxxxx");

    //
    //
    $sql = "SELECT * FROM Kundregistert ORDER BY ID DESC;";
    //
    //
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    $row = mysqli_fetch_assoc($result);
    //
    // Avsluta anslutning till db
    return $row['Namn'];
    mysqli_close($conn);
 }
<小时/>
    function __construct($companyname){
    $this->foretag = $companyname;
    $this->CustomerList = unserialize($this->read_data());
 }

我感觉自己迷失在代码中

最佳答案

首先,您的 php 方法 getCustomerList 应如下修改;

public function getCustomerList()
{
    $sql = "SELECT * FROM Kundregistert ORDER BY ID DESC;";
    //
   //
    $result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

    $allRows = array();
    while($row = mysqli_fetch_assoc($result))
    {
        $allRows[] = $row;
    }
    //
    // Avsluta anslutning till db
    // Return statement should be like below after mysqli_close
    //return ($row['Namn'], $row['Telefonnummer'], $row['Epost'], $row['Dacktyp'], $row['Lagerstatus'], $row['Datum']);
    mysqli_close($conn);

    return $allRows;
}

正如你在上面看到的,mysqli 查询结果应该根据你的情况迭代并收集在一个数组中。

谢谢您可以轻松地在结果中进行 foreach 操作,如下所示;

foreach($company->getCustomerList() as $key => $obj){
    echo "<h3 id='id'>$key</h3> " . $obj->getcustomername() . ", " . $obj->getcustomertfn() . ", " . $obj->getcustomerepost() . "<br>" . $obj->gettirebrand() . ", " . $obj->getstatus() . ": " . $obj->getdate() . "<br>"." Radera post $key " . " Uppdatera Post";}

请注意 $key 将始终是数组当前迭代的索引。还要注意 $obj 不会是一个具有像 getcustomerepost() 等成员的类。对于这种使用,您应该从您的方法中返回一个带有数据的对象。

关于php - mysqli_fetch_assoc 返回为 foreach() 提供的参数无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34963969/

相关文章:

php - 创建预填充的 MySQL 表

mysql - 我收到错误 #1329 - 无数据 - 在 MySQL 中获取、选择或处理的行为零

java - MySQL 不使用 netbeans 将插入推送到数据库中

php - 如何组合具有不同数组结果的 2 个 Foreach 循环?

php - 如何在 INSERT 中执行 foreach?

php - 在 MySQL 中存储嵌套导航菜单

php - Laravel - 无法更改默认缓存驱动程序

php - 我想更新两个表中的值

mysql - NoSQL 投票系统

spring - 对于 Thymeleaf 中的每个运算符