-1

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.

arkascha
  • 41,620
  • 7
  • 58
  • 90
bbqpauk
  • 1
  • 3
  • I cannot spot any printing command in there... – arkascha Jun 20 '15 at 16:09
  • 2
    You don't echo the array, you echo individual elements from the array; `foreach($array as $teams=>$team){ echo $team['Team']; }` – Mark Baker Jun 20 '15 at 16:10
  • You are open to SQL injections. You are using mysqli so use the prepared functionality of it. 1. http://php.net/manual/en/mysqli.prepare.php 2. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – chris85 Jun 20 '15 at 16:15
  • @MarkBaker OMG BLESS UR SOUL IT WORKED BLESS – bbqpauk Jun 20 '15 at 16:21

1 Answers1

0

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>";
    }
} 
PHPhil
  • 1,555
  • 13
  • 27