我的代码继续打印“#34; array"而不是数据库信息。当我尝试使用print_r()
或var_dump()
时,它会回复有关数组的所有信息
这是var_dump
array(1){[" Team"] => string(8)" Oakville" } array(1){[" Team"] => string(8)" Brampton" } array(1){[" Team"] => string(7)" Toronto" } array(1){[" Team"] => string(11)" Mississauga" }
这是我的代码
if(isset($_REQUEST['submit2'])){
$leaguename = $_POST['league'];
$db_host = '**';
$db_user = '**';
$db_pwd = '**';
$database = '**';
$db = new mysqli($db_host, $db_user, $db_pwd, $database);
$sql = "SELECT Team FROM Games WHERE League = '$leaguename' AND Team <> ''";
$result = mysqli_query($db, $sql) or die ("no query");
if (!$result) {
die('Invalid query: ' . mysqli_error());
}
$array = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
$array[] = $row;
}
$teamnumber=count($array);
echo $teamnumber . "<br>";
foreach($array as $teams=>$team){
var_dump($team);
}
我试图打印从数据库中取出并放入阵列的运动队名称。
答案 0 :(得分:0)
您的数组是一个多维数组,一个包含一个或多个数组的数组。
因为您正在打印外部数组var_dump()
。你应该使用双循环:
foreach($array as $teams){
foreach($teams as $team){
echo $team . "<br>";
}
}