我连接到数据库,我使用mysqli_fetch_assoc
来创建一个assosiative数组,以将数据返回到屏幕。这部分代码如下所示:
$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']
,因为它只有1列),然后它没有提供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&#39;”以外的其他内容,然后我也遇到了foreach
错误。所以我认为我如何尝试返回列有问题,但我似乎无法使其工作。
我是php的初学者,请记住这一点。
感谢。
EDIT *****
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());
}
我觉得我在代码中丢失了
答案 0 :(得分:0)
首先,你的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($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()等成员的类。对于那种使用,你应该使用你的数据从你的方法返回一个对象。
答案 1 :(得分:0)
可能是您可以尝试此代码
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)) {
// Add data from database to variable $array
$array[]['Namn'] = $row['Namn'];
$array[]['Telefonnummer'] = $row['Telefonnummer'];
$array[]['Epost'] = $row['Epost'];
$array[]['Dacktyp'] = $row['Dacktyp'];
$array[]['Lagerstatus'] = $row['Lagerstatus'];
$array[]['Datum'] = $row['Datum'];
}
// Load with this code
for($i=0; $i<count($array); $i++) {
echo $array[$i]['Namn']."<br>";
echo $array[$i]['Telefonnummer']."<br>";
echo $array[$i]['Epost']."<br>";
echo $array[$i]['Dacktyp']."<br>";
echo $array[$i]['Lagerstatus']."<br>";
echo $array[$i]['Datum']."<br>";
}