简体   繁体   English

从SELECT * FROM $表中排除字段

[英]Exclude a field from a SELECT * FROM $table

I need to exclude a field 'password' from a SELECT * FROM $table; 我需要从SELECT * FROM $table;排除字段'password' SELECT * FROM $table; where $table is a PHP variable. 其中$table是一个PHP变量。

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. 对不起,如果它有点乱,我只是开始学习编程。

I understand that you're wanting to have a single PHP function that will return all the results in a given table. 我知道您希望拥有一个PHP函数,它将返回给定表中的所有结果。 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. 也许不是返回$resul变量并在返回后解析数据,而是应该在返回之前将其解析为关联数组。 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. “myData”是非描述性的,因此对于数据访问功能来说是非常糟糕的名称。

Also, if you're going to name a variable $resul , just go ahead and type the "t" and name it $result FFS. 此外,如果您要命名变量$resul ,请继续输入“t”并将其命名为$result FFS。

In the foreach loop, get the key and the data from the array. 在foreach循环中,从数组中获取密钥数据 (The current code is getting only the data.) (当前的代码是越来越只有数据)。

Inside the foreach loop, do a conditional test on the value of key. 在foreach循环内部,对key的值进行条件测试。

If the value of the key matches "password", then skip over outputting anything for that element of the array. 如果键的值与“password”匹配,则跳过输出该数组元素的任何内容。 If it doesn't match key, then output it (like the current code is doing.) 如果它与key不匹配,则输出它(就像当前代码正在做的那样。)


Look at the alternative syntax for foreach: 查看foreach的替代语法:

References: http://php.net/manual/en/control-structures.foreach.php 参考文献: http//php.net/manual/en/control-structures.foreach.php

And for simple conditional tests 对于简单的条件测试

Reference: http://php.net/manual/en/control-structures.if.php 参考: 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. 考虑是否要匹配“密码”,“密码”,“密码”等。您可能需要不区分大小写的匹配。

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

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

IMO i think the simplest way in this case is to use special case for php IMO我认为在这种情况下最简单的方法是使用php的special case

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. 无需更多功能或在数据库的INFORMATION_SCHEMA中搜索以查找列密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM