2

这是我的数组:

protected static $db_fields = array('id', 'username', 'password');

这是我如何使用它:

protected function attributes(){
    $attributes = array();
    foreach(static::$db_fields as $field){
        $attributes[$field] = $this->$field;
    }
    return $attributes;
}

这是我使用属性的方式:

protected function create(){
    global $database;
    $attributes = $this->sanitized_attributes();

    $sql = "INSERT INTO " . static::$table_name . " (";
    $sql .= join(", ", array_keys($attributes));
    $sql .= ") VALUES ('";
    $sql .= join("', '", array_values($attributes));
    $sql .= "')";

    if($database->query($sql)){
        $this->id = $database->inserted_id();
        return TRUE;
    }else{
        return FALSE;
    }
}

我需要做的就是直接从数据库中生成 db_fields 数组,而不是像我所做的那样手动分配它。我玩弄了“SHOW COLUMNS FROM”表名,但没有任何运气。

显示列:

function attributes(){
    global $database;
    $attributes = array();
    $sql = "SHOW COLUMNS FROM " . static::$table_name;
    $result = $database->query($sql);
    $db_fields = mysql_fetch_assoc($result);

    for($i = 0; $i < mysql_num_rows($result); $i++){
        while ($db_fields = mysql_fetch_assoc($result)) {
          $attributes[] = $db_fields['Field'];
        }
    }

    return $attributes;
}

我的最终方法应该是一个数组,如:

$db_fields = ('column1_name', 'column2_name', 'column3_name') 

我当前的“SHOW COLUMNS”播放的结果是它使用户名的值:用户名,密码:密码......就像列的名称一样。

我相信这是我如何将这些列名称分配给我的数组的问题。那么制作这样一个数组的正确查询是什么?

最后我解决了它:

protected static $db_fields;

function __construct(){
    self::generate_attributes();
}

protected static function generate_attributes(){
    $sql = "SHOW COLUMNS FROM admins";
    $result = mysql_query($sql);
    $rows = mysql_fetch_assoc($result);

    for($i = 0; $i < mysql_num_rows($result); $i++){
        while ($rows = mysql_fetch_assoc($result)) {
          self::$db_fields[] = $rows['Field'];
        }
    }
    return self::$db_fields;
}
protected function attributes(){
    $attributes = array();
    foreach(static::$db_fields as $field){
        $attributes[$field] = $this->$field;
    }
    return $attributes;
}

感谢大家尝试提供帮助。

4

4 回答 4

1

试试怎么样

SELECT `column_name` FROM `information_schema`.COLUMNS WHERE TABLE_NAME = 'yourtable';

您的 db 用户将需要对 information_schema 的读取访问权限。

如果需要,您还可以使用 GROUP_CONCAT 创建逗号分隔的字符串。

SELECT GROUP_CONCAT(c.column_name) FROM `information_schema`.COLUMNS c WHERE TABLE_NAME = 'yourtable';
于 2013-03-14T05:07:59.843 回答
0

尝试这个:

function attributes(){
    global $database;
    $attributes = array();
    $sql = "SELECT column1_name,column2_name,column3_name FROM " . static::$table_name;//your columns
    $result = $database->query($sql);
    $db_fields = array('column1_name', 'column2_name', 'column3_name') ;
    $i=0;
    if(mysql_num_rows($result)){
        while ($rows = mysql_fetch_row($result)) {
           for($j=0;$j<count($db_fields);$j++)
           {
               $attributes[$i][$db_fields[$j]] = $rows[$j] ;
           }
           $i++;
        }
    }
    return $attributes;
}
于 2013-03-14T05:32:31.567 回答
0

尝试这些使用您的表名和架构来获取有关列的所有详细信息。

SELECT `column_name`  FROM `information_schema`.COLUMNS WHERE TABLE_NAME = 'table_name' AND TABLE_SCHEMA = 'database_name'

你可以使用*而不是column_name

检查 inaformation_schema 表,它有很多要知道的

于 2013-03-14T06:19:17.870 回答
0
$query = "SELECT * from myTable LIMIT 1";
if ($result = mysqli_query($link, $query)) {
$finfo = mysqli_fetch_fields($result);

foreach ($finfo as $val) {
   print_r($val);
}
mysqli_free_result($result);
}
于 2013-03-14T05:05:59.643 回答