miguelmald miguelmald - 7 months ago 6
SQL Question

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.

Answer

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;
}