The word “array” prints instead of databse information

风流意气都作罢 提交于 2019-12-23 06:22:18

问题


My code keeps on print the word "array" instead of the database information. When i try to use print_r() or var_dump() it echos all the information about the array

this is with 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" }

Here's my code

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);
}

I'm trying to print the names of sports teams that are being fetched from a database and put into an array.


回答1:


Your array is a Multidimensional Arrays, an array containing one or more arrays.

Because of that you are printing the outer array var_dump(). You should use a double loop:

foreach($array as $teams){
    foreach($teams as $team){
        echo $team . "<br>";
    }
} 


来源:https://stackoverflow.com/questions/30956106/the-word-array-prints-instead-of-databse-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!