1

I am using a query ("SHOW COLUMNS FROM XYZ_table"). It displays the out like

Array ( [Field] => policyID [Type] => int(10) [Null] => YES [Key] => [Default] => [Extra] => ).

I am only concerned with PolicyID with it's type => int and size 10. How can I ignore other irrelevant data ? Here is a code: /* $i has size of this array "$tb_names" */

 while ($i!=0){
                $result = mysql_query("SHOW COLUMNS FROM $tb_names[$j] ");
        if (!$result) {
            echo 'Could not run query: ' . mysql_error();
        exit;
        }
        if (mysql_num_rows($result) > 0) {
            while ($row = mysql_fetch_assoc($result)) {
            $var = print_r($row,true);
            echo $var."<br>";

        }
    }       
            $i--;
            $j++;


    }
4
  • What is the problem having extra fields within array? Or you can simply uset() those from array. Commented Aug 11, 2016 at 6:04
  • it's a requirement not to get extra fields , I need to parse them Commented Aug 11, 2016 at 6:06
  • You must unset them in PHP, transpose the array in PHP or change your SQL to return only what you want included. Commented Aug 11, 2016 at 6:08
  • @pcnate, Alok Patel : Can I parse the reuslt using php ? Commented Aug 11, 2016 at 6:14

2 Answers 2

2

Hi You can try this query

SELECT column_name,character_maximum_length , data_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA =  'databasename'
AND TABLE_NAME =  'tablename';

for specific column

SELECT column_name, character_maximum_length, data_type
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA =  'databasename'
AND TABLE_NAME =  'tablename'
AND column_name =  'columnname';

Without database name

SELECT column_name , data_type ,character_maximum_length FROM information_schema.columns
WHERE table_name = 'tablename';

For int length please use NUMERIC_PRECISION

SELECT column_name , data_type ,NUMERIC_PRECISION FROM information_schema.columns
    WHERE table_name = 'tablename';
Sign up to request clarification or add additional context in comments.

10 Comments

thnak you soo much it helped me going more close to the solution. here is the output: "Array ( [column_name] => policyID [character_maximum_length] => [data_type] => int ) " why doesn't it gives me the lenght which I have defined like 10 in this case
@Raja use COLUMN_TYPE instead
@Raja Please use NUMERIC_PRECISION for int type length
@user1234 : yes it worked somehow. But I need to parse it more. like Col_Name TYPE Size PolicyID INT 10
@Raja then you shuld use this query SELECT column_name AS Col_Name , data_type AS TYPE ,NUMERIC_PRECISION AS Size FROM information_schema.columns WHERE table_name = 'tablename'
|
0

You'll have to modify to suit your usecase, but try something along the lines of this:

$allowedColumns = array(
    'columna',
    'columnb',
    'columnc',
    'columnd',
);

while( $i!=0 ) {
  $result = mysql_query("SHOW COLUMNS FROM $tb_names[$j] ");

  if( !$result ) {
    echo 'Could not run query: ' . mysql_error();
    exit;
  }
  if( mysql_num_rows($result) > 0 ) {
    while( $row = mysql_fetch_assoc($result) ) {
      $columns = array();
      foreach( $row as $column ) {
        if( in_array( $column, $allowedColumns ) ) {
          $columns[] = $column;
        }
      }
      $var = print_r($columns,true);
      echo $var."<br>";
    }
  }       
  $i--;
  $j++;
}

3 Comments

I am going to try ur sol
If it works, mark as answer, if not ask more questions
I tried ur sol but it ain't working. thanks for the effort.

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.