通过函数选择查询..从db获取数据

How do I fetch data from db using select query in a function?

Example

function ec_select_query($student, $row = '', $fields=array()) {
    $qry = "SELECT * FROM student";
    $qry = mysql_query($qry);

    while($row = mysql_fetch_assoc($qry)){}

    return $row;
}

If you want to return all rows then first save it in an array in while loop then return this array.

function ec_select_query($student,$row='',$fields=array())
{
   $qry = "SELECT * FROM student";
   $qry = mysql_query($qry);
   $result = array();   
   while($row = mysql_fetch_assoc($qry))
   {
       $result[] = $row;
   }     
    return $result;
 }

This code might help you :

 function ec_select_query($student,$row='',$fields=array())
 {
    $q = "SELECT * FROM student";
    $q = mysql_query($qry);
    while($row = mysql_fetch_array($qry))
    {
        return $row;
    } 
 }

Try it

function select_query($table, $where=array(),$fields=array()){

    $select_fields = $table."*";

    if(!empty($fields) && is_array($fields)){
        $select_fields = implode(",", $fields);
    }

    $sql = "select ".$select_fields." from ".$table." where 1=1 ";

    if(!empty($where) && is_array($where)){
        foreach ($where as $key => $value) {
            $sql .= " AND ".$value;
        }
    }
    $query = mysql_query($sql);
    $result = array();
    while($row = mysql_fetch_assoc($result)){
        $result[] = $row;
    }
    return $result;
}

Call Function

$fields = array("id","name","city");
$where = array('name = "abc"','city like "aaa"');

$students = select_query("studendts", $where, $fields);

It is easiest way to produce entire data in array

 function db_set_recordset($sql) {
        $qry = mysql_query($sql);
        $row= array();
        while($out = mysql_fetch_assoc($qry)) {

                $row[] = $out;

        }
        return $row;
} 

$qry     = "SELECT * FROM student";
$result  = db_set_recordset($qry);

Its is running code. Modify it according to your needs

$con = mysql_connect('localhost','root','') or die("Unable to connect to MySQL");
mysql_select_db('demo', $con) or die("Database not found");

function ec_select_query($student)
 {
   $query = "SELECT * FROM $student";
   $result = mysql_query($query);
   $row = array();
   $getData = array();
   while($row = mysql_fetch_array($result))
    {
       $getData[]=$row;
    } 
    return $getData;
 }
$information = ec_select_query('accountplans');
echo "<pre>"; print_r($information); die;