尝试读取数组上的 属性
Attempt to read property on array
我收到这个错误:
Warning: Attempt to read property "squadra" on array in C:\xampp\htdocs\statistiche-calcio\index.php on line 57
这是我的 db.php 文件的代码:
<?php
function connect($dbHost, $dbName, $dbUsername, $dbPassword){
$db = new mysqli(
$dbHost,
$dbUsername,
$dbPassword,
$dbName
);
if($db->connect_error){
die("Impossibile connettersi al database \n" . $db->connect_error . "\n" . $db->connect_errno);
}
return $db;
}
function fetchAll(mysqli $db, $table){
$data = [];
$sql = "SELECT * FROM $table";
$results = $db->query($sql);
if($results->num_rows > 0){
while($row = $results->fetch_object()){
$data[] = $row;
}
}
return $data;
}
function fetchSquadra(mysqli $db, $table, $squadra){
$data = [];
$sql = "SELECT * FROM $table WHERE id = " . $squadra;
$results = $db->query($sql);
if($results->num_rows === 1){
while($row = $results->fetch_object()){
$data[] = $row;
}
}
return $data;
}
这是我的 index.php 我得到错误的地方:
<?php
require_once('config.php');
require_once('db.php');
$db = connect(
DB_HOST,
DB_NAME,
DB_USERNAME,
DB_PASSWORD,
);
$records = [];
$records = fetchAll($db, 'partite');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
table, th, td{
border: 1px solid grey;
text-align: center;
}
</style>
<h1>Risultati Premier League</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Data</th>
<th>Casa</th>
<th>Risultato</th>
<th>Ospite</th>
<th>Risultato 1 tempo</th>
</tr>
</thead>
<tbody>
<?php
if(count($records) > 0):
foreach($records as $record):
$squadra_casa = fetchSquadra($db, 'squadre', $record->squadra_casa);
$squadra_ospite = fetchSquadra($db, 'squadre', $record->squadra_ospite);
?>
<tr>
<td><?php echo $record->id;?></td>
<td><?php echo $record->data;?></td>
<td><?php echo $squadra_casa->squadra;?></td>
<td><?php echo $record->goal_casa . " - ". $record->goal_ospite;?></td>
<td><?php echo $squadra_ospite->squadra;?></td>
<td><?php echo $record->goal_casa_1t . " - ". $record->goal_ospite_1t;?></td>
</tr>
<?php
endforeach;
else: ?>
<tr>
<td colspan="6">Non è stata trovata nessuna partita!</td>
</tr>
<?php endif ?>
</tbody>
</table>
</body>
</html>
我已经尝试 var_dump() 变量 $squadra_casa 并且我得到了这个:
array(1) {
[0]=>
object(stdClass)#5 (3) {
["id"]=>
string(2) "18"
["squadra"]=>
string(6) "Fulham"
["campionato"]=>
string(1) "1"
}
}
但是当我尝试回显 $squadra_casa->squadra 属性 时,我得到了错误。
我该如何解决?我做错了什么?
你应该试试 $squadra_casa[0]->squadra
我收到这个错误:
Warning: Attempt to read property "squadra" on array in C:\xampp\htdocs\statistiche-calcio\index.php on line 57
这是我的 db.php 文件的代码:
<?php
function connect($dbHost, $dbName, $dbUsername, $dbPassword){
$db = new mysqli(
$dbHost,
$dbUsername,
$dbPassword,
$dbName
);
if($db->connect_error){
die("Impossibile connettersi al database \n" . $db->connect_error . "\n" . $db->connect_errno);
}
return $db;
}
function fetchAll(mysqli $db, $table){
$data = [];
$sql = "SELECT * FROM $table";
$results = $db->query($sql);
if($results->num_rows > 0){
while($row = $results->fetch_object()){
$data[] = $row;
}
}
return $data;
}
function fetchSquadra(mysqli $db, $table, $squadra){
$data = [];
$sql = "SELECT * FROM $table WHERE id = " . $squadra;
$results = $db->query($sql);
if($results->num_rows === 1){
while($row = $results->fetch_object()){
$data[] = $row;
}
}
return $data;
}
这是我的 index.php 我得到错误的地方:
<?php
require_once('config.php');
require_once('db.php');
$db = connect(
DB_HOST,
DB_NAME,
DB_USERNAME,
DB_PASSWORD,
);
$records = [];
$records = fetchAll($db, 'partite');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
table, th, td{
border: 1px solid grey;
text-align: center;
}
</style>
<h1>Risultati Premier League</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Data</th>
<th>Casa</th>
<th>Risultato</th>
<th>Ospite</th>
<th>Risultato 1 tempo</th>
</tr>
</thead>
<tbody>
<?php
if(count($records) > 0):
foreach($records as $record):
$squadra_casa = fetchSquadra($db, 'squadre', $record->squadra_casa);
$squadra_ospite = fetchSquadra($db, 'squadre', $record->squadra_ospite);
?>
<tr>
<td><?php echo $record->id;?></td>
<td><?php echo $record->data;?></td>
<td><?php echo $squadra_casa->squadra;?></td>
<td><?php echo $record->goal_casa . " - ". $record->goal_ospite;?></td>
<td><?php echo $squadra_ospite->squadra;?></td>
<td><?php echo $record->goal_casa_1t . " - ". $record->goal_ospite_1t;?></td>
</tr>
<?php
endforeach;
else: ?>
<tr>
<td colspan="6">Non è stata trovata nessuna partita!</td>
</tr>
<?php endif ?>
</tbody>
</table>
</body>
</html>
我已经尝试 var_dump() 变量 $squadra_casa 并且我得到了这个:
array(1) { [0]=> object(stdClass)#5 (3) { ["id"]=> string(2) "18" ["squadra"]=> string(6) "Fulham" ["campionato"]=> string(1) "1" } }
但是当我尝试回显 $squadra_casa->squadra 属性 时,我得到了错误。
我该如何解决?我做错了什么?
你应该试试 $squadra_casa[0]->squadra