Results 1 to 4 of 4

Thread: Exclude mysql column from array

  1. #1
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exclude mysql column from array

    I have a function to reset all columns of a mysql database in php. I however want to exclude some columns from this reset. Need som help...

    PHP Code:
    $qColumnNames mysql_query("SHOW COLUMNS FROM $table") or die("mysql error");
    $numColumns mysql_num_rows($qColumnNames);
    $x 0;
    while (
    $x $numColumns)
    {
        
    $colname mysql_fetch_row($qColumnNames);
        
    $col[$colname[0]] = $colname[0];
        
    $x++;
    }
    $querycx 'UPDATE '.$table.' SET 'implode'=\'\', '$col ) .'=\'\' WHERE `userName` = \''$user2 .'\'';

    mysql_query($querycx); 

  2. #2
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You should be able to call each colname by using an index e.g. [0] or [3], you can use an if and else to disable sifting through certain columns. e.g.

    This should be in between the while brackets.

    PHP Code:
    if ($colname[1] || $colname[5]) {

    } else {

    $querycx 'UPDATE '.$table.' SET 'implode'=\'\', '$col ) .'=\'\' WHERE `userName` = \''$user2 .'\''

    mysql_query($querycx); 



  3. #3
    Join Date
    Jun 2007
    Posts
    50
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Solved it now:

    PHP Code:
    <?php

    include ('mysqlconnnection.php'); //include connection

    $arrExclude = array('userId''userName''userPass''firma''orgnr''Scroll');

    $result mysql_query("SHOW COLUMNS FROM $table") or die("mysql error");


    $numColumns mysql_num_rows($result);

    $x 0;
    while (
    $x $numColumns)
    {
        
    $colname mysql_fetch_row($result);
        if(!
    in_array($colname$arrExclude)) {
            
    $col[$colname[0]] = $colname[0];
        }    
        
    $x++;
    }


    $col2array_diff($col,$arrExclude); //removes arrExclude from the created array $col

    $querycx 'UPDATE '.$table.' SET 'implode'=\'\', '$col2 ) .'=\'\''//new array is used in mysql query

    mysql_query($querycx);



    ?>

  4. #4
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    This is really the long way around the problem. Use SQL to get the column names you want. Here is the query you would need. I'm not really sure what you want to do with this data though.
    Code:
    SELECT column_name FROM information_schema.columns WHERE table_name = '$table' AND column_name NOT IN('userId', 'userName', 'userPass', 'firma', 'orgnr', 'Scroll')

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •