How to loop through an array return from the Query of Mysql

Posted by Jerry on Stack Overflow See other posts from Stack Overflow or by Jerry
Published on 2010-05-18T21:02:09Z Indexed on 2010/05/18 21:10 UTC
Read the original article Hit count: 143

Filed under:
|
|
|
|

This might be easy for you guys but i could't get it.

I have a php class that query the database and return the query result. I assign the result to an array and wants to use it on my main.php script. I have tried to use echo $var[0] or echo $var[1] but the output are 'array' instead of my value. Anyone can help me about this issue? Thanks a lot!

My php class

<?php 
class teamQuery {

            function teamQuery(){


            }
            function getAllTeam(){
                $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
                    if (!$connection) {
                        die("Database connection failed: " . mysql_error());
                    }

                    $db_select = mysql_select_db(DB_NAME,$connection);
                    if (!$db_select) {
                        die("Database selection failed: " . mysql_error());
                    }

                  $teamQuery=mysql_query("SELECT * FROM team", $connection);

                    if (!$teamQuery){
                        die("database has errors: ".mysql_error());
                          }
                    $ret = array();   

                    while($row=mysql_fetch_array($teamQuery)){

                    $ret[]=$row;
                    }     
                    mysql_free_result($teamQuery); 

                    return $ret;
                    }


            }


?>

My php on the main.php

$getTeam=new teamQuery();
$team=$getTeam->getAllTeam();

   //echo $team[0] or team[1] output 'array' string!
  // while($team){
  // do something }  can't work either

  // How to loop through the values??

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about array