miguelmald
miguelmald

Reputation: 70

Exclude a field from a SELECT * FROM $table

I need to exclude a field 'password' from a SELECT * FROM $table; where $table is a PHP variable.

Only one of the four posible tables has a 'password' field.

Here is what I have:

function myData($table)
{
  include "conf.php";
  $con   = mysqli_connect($host, $user, $password, $db);
  $sql   = "SELECT * FROM $table;";
  $resul = mysqli_query($con, $sql);
  return $resul;
}

Any ideas?

EDIT: This is how I treat the data returned:

$resulFields    =   myFields($table);
$resulData      =   myData($table);

while ($fields = mysqli_fetch_array($resulFields, MYSQLI_ASSOC)) {
    $field      =   $fields['Field'];
    $header  .=   "<th>$field</th>";

    while ($data_array = mysqli_fetch_array($resulData, MYSQLI_ASSOC) ) {
        $body .=  "<tr id='$data_array[id]'>";
        foreach ($data_array as $data){
            $body .= "<td>$data</td>";
        }
    }
}

Sorry if it's a little bit messy, I'm just starting to learn programming.

Upvotes: 1

Views: 1827

Answers (5)

spencer7593
spencer7593

Reputation: 108370

In the foreach loop, get the key and the data from the array. (The current code is getting only the data.)

Inside the foreach loop, do a conditional test on the value of key.

If the value of the key matches "password", then skip over outputting anything for that element of the array. If it doesn't match key, then output it (like the current code is doing.)


Look at the alternative syntax for foreach:

References: http://php.net/manual/en/control-structures.foreach.php

And for simple conditional tests

Reference: http://php.net/manual/en/control-structures.if.php

Consider whether you want to match "password", "PASSWORD", "Password", etc. You might want a case insensitive match.

Upvotes: 1

erahm
erahm

Reputation: 344

I understand that you're wanting to have a single PHP function that will return all the results in a given table. Perhaps instead of returning the $resul variable and parsing the data after the return, you should parse it into an associative array prior to returning it. You can try something like this:

function myData($table) {
    include "conf.php";
    $con =   mysqli_connect($host, $user, $password, $db);
    $sql =   "SELECT * FROM {$table}";

    $resul =  mysqli_query($con, $sql);
    $row = $resul->fetch_assoc();
    unset( $row['password'] );

    return $resul;
}

Though I feel it's important to note that in the interests of proper coding practices and single responsibility, you should have specific data access functions for each query you wish to run. I don't recommend having a single function that just returns everything from a table. Functions should also be named such that you know what they're doing. "myData" is very non-descriptive and as such a very poor name for a data access function.

Also, if you're going to name a variable $resul, just go ahead and type the "t" and name it $result FFS.

Upvotes: 2

Pham X. Bach
Pham X. Bach

Reputation: 5432

IMO i think the simplest way in this case is to use special case for php

function myData($table) {
    include "conf.php";
    $con  =   mysqli_connect($host, $user, $password, $db);
    if($table == "special_case_table"){
        $sql    =   "SELECT col1, col2, col3 FROM special_case_table;"; //except "password" column
    }
    else  $sql    =   "SELECT * FROM $table;";
    $resul  =   mysqli_query($con, $sql);
    return $resul;

No need more function or go search in INFORMATION_SCHEMA of database to find column password.

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

Obviously the best bet is to select what you do want:

SELECT id, name, whatever FROM $table

Hopefully I'm not going down the wrong path, but here is one way other than querying for the fields and removing the password:

$columns['tableA'] = array('id', 'name', 'whatever');
$columns['tableB'] = array('id', 'user', 'something');

Then do:

$select = implode(',', $columns[$table]);
$sql = "SELECT $select FROM $table";

Upvotes: 0

Adri&#225;n
Adri&#225;n

Reputation: 419

Maybe you can do something like this:

function myData($table) {
    include "conf.php";
    $con =   mysqli_connect($host, $user, $password, $db);
    $sql =   "SELECT field1, field2";

    if ($table == "TABLE_WITH_PASSWORD") {
        $sql.=",password";
    }
    $sql.=" FROM $table;";

    $resul =  mysqli_query($con, $sql);
    return $resul;
}

Upvotes: 0

Related Questions