0

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;
}
1
  • Put your question with some sort of well formatted code man? Commented Mar 27, 2015 at 6:06

5 Answers 5

1

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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Could you please edit your answer to give an explanation of why this code answers the question? Code-only answers are discouraged, because they don't teach the solution.
1

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;

Comments

0

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.