0

I have a function which selects the * values from a particular table, I need to configure that if I pass * in parameter then it selects all or the selected parameters.

My function is as follows:

  public function select($tablename){
          $select = mysql_query("SELECT * FROM $tablename");
          if(!$select){
              echo "cant select table mane";
              }
          while ($row = mysql_fetch_array($select)){

              print_r($row);
              }
          }      

How can I pass the dynamic colum name in place of *

Alex
  • 1,416
  • 4
  • 16
  • 42
user2106353
  • 1,191
  • 3
  • 10
  • 18

6 Answers6

1
public function select($tablename,$column=NULL){
      if($column == null) {
          $select = mysql_query("SELECT *  FROM $tablename");
      } else {
          $select = mysql_query("SELECT ".$column."  FROM $tablename");
      }

call it using select("abcd_table","id,name"); OR for all just select("abcd_table");

By Alex

Avoid mysql, it is deprecated. Use mysqli or PDO instead
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

Add one more parameter $columnname type array in your function.

   public function select($tablename,$columnname = array()){
      if(count($columnname)){
         $columnname = implode(",",$columnname);
         $select = mysql_query("SELECT {$columnname} FROM $tablename");
      }  
      else{
        $select = mysql_query("SELECT * FROM $tablename");
      }
      #code continue
   }

Calling will be like below

To select all fields,

select($tablename);

To select some fields,

select($tablename,array('field','field2'));

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rikesh
  • 26,156
  • 14
  • 79
  • 87
1

That's even easier:

public function select( $tablename , $columnName = array() ){
   $columnName = $columnName ? implode( ',' , $columnName ) : '*';
   $select     = mysql_query( 'SELECT ' . $columnName . ' FROM ' . $tableName );
   if(!$select){
      echo "cant select table mane";
   }
   while ($row = mysql_fetch_array($select)){
      print_r($row);
   }
}
0
 public function select($tablename,$column="*"){

          $select = mysql_query("SELECT $column FROM $tablename");
          if(!$select){
              echo "cant select table mane";
              }
          while ($row = mysql_fetch_array($select)){

              print_r($row);
              }
          }      

call like select("table","*") or call like select("table","col1,col2")

user7282
  • 5,106
  • 9
  • 41
  • 72
0
public function select($tablename, $selectrow){
          $select = mysql_query("SELECT $selectrow FROM $tablename");
          if(!$select){
              echo "cant select table mane";
          }
          while ($row = mysql_fetch_array($select)){
              print_r($row);
          }
}      
suspectus
  • 16,548
  • 8
  • 49
  • 57
0

try this modified function

public function select($tablename,$array_fields){
    if(!empty($array_fields)){
      $fieldstr = implode(',',$array_fields);
      $fieldstr = trim($fieldstr,',');
    }else{
        $fieldstr = '*';
    }
      $select = mysql_query("SELECT $fieldstr FROM $tablename");
      if(!$select){
          echo "cant select table mane";
          }
      while ($row = mysql_fetch_array($select)){

          print_r($row);
          }

}

Prashant Shukla
  • 329
  • 1
  • 2
  • 17