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,336
  • 4
  • 14
  • 38
user2106353
  • 1,061
  • 2
  • 10
  • 17

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,136
  • 18
  • 69
  • 98
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 stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Rikesh
  • 25,621
  • 14
  • 77
  • 86
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,269
  • 8
  • 39
  • 69
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
  • 15,729
  • 8
  • 46
  • 54
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
  • 16