简体   繁体   English

将多个相似字段放置在多维数组中-PHP mysql

[英]Place multiple similar fields in multi-dimensional array - php mysql

I want to execute an SQL query like: 我想执行一个SQL查询,如:

select 'tb1'.'f1','tb1'.'f2','tb2'.'f1' from 'tb1','tb2';

Now the problem is that i want to put it into an array in PHP like: 现在的问题是我想将其放入PHP数组中,例如:

$result['tb1']['f1'], $result['tb1']['f2'], $result['tb2']['f1']...

Any idea how to achieve the above? 任何想法如何实现以上目标? Afaik there is no function which does the above. Afaik没有上述功能。 I was wondering the best simple way to do it. 我想知道最好的简单方法。 I do not want to use a query like "select .. as .. " unless necessary. 除非必要,否则我不想使用“选择..作为..”这样的查询。

I do not know in advance what the fields will be, so I cannot assign them manually as suggest by the answer by benlumley. 我事先不知道这些字段是什么,因此我无法按照benlumley的答案建议手动分配它们。

Thank you, Alec 谢谢亚历克

You'll need to select the data as you are already doing, and then loop over it getting it into the format required, and because the fields have the same names, its easiest to use aliases or they'll just overwrite each other in the returned data (but you could use mysql_fetch_row instead, which returns a numerically indexed array). 您需要像已经做的那样选择数据,然后将其循环以使其成为所需的格式,并且由于字段名称相同,因此最容易使用别名,否则它们将相互覆盖。返回的数据(但是您可以使用mysql_fetch_row代替,它返回一个数字索引数组)。

For example: 例如:

$sql = "select tb1.f1 as tb1f1,tb1.f2 as tb1f2,tb2.f1 as tb2f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
    $result['t1']['f1']=$row['tb1f1'];
    $result['t1']['f2']=$row['tb1f2'];
    $result['t2']['f1']=$row['tb2f1'];
}

(The quoting was wrong in your sql as well) (该引号在您的sql中也是错误的)

That won't handle multiple rows either, but your question sort of implies that you are only ever expecting one row? 那也不会处理多行,但是您的问题有点暗示您只希望有一行?

WIthout aliases: 别名:

$sql = "select tb1.f1,tb1.f2,tb2.f1 from tb1,tb2";
$result = mysql_query($sql);
while ($row = mysql_fetch_row($result)) {
    $result['t1']['f1']=$row[0];
    $result['t1']['f2']=$row[1];
    $result['t2']['f1']=$row[2];
}

I prefer the first version unless you have a good reason to use the second, as its less likely to result in errors if you ever change the sql or add fields etc. 我更喜欢第一个版本,除非您有充分的理由使用第二个版本,因为如果您更改sql或添加字段等,则第二个版本不太可能导致错误。

EDIT: 编辑:

Taking the meta data idea from the response below .... 从下面的响应中获取元数据的想法...。

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
$meta = array();
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
  $meta[$i] = mysql_fetch_field($result, $i);
}
while ($row = mysql_fetch_row($result)) {
   foreach($row as $key=>$value) {
     $out[$meta[$key]->table][$meta[$key]->name]=$value;
   }
}

seems to do exactly what you are after - although you can still only get one row at a time. 似乎完全可以满足您的要求-尽管您一次只能获得一行。

Easily updated to store multiple rows with another dimension on the array: 轻松更新以在数组上存储具有另一维的多行:

Change: 更改:

$out[$meta[$key]->table][$meta[$key]->name]=$value;

To: 至:

$out[][$meta[$key]->table][$meta[$key]->name]=$value;

Since you say you can't specify column aliases, and you can't know the fields of the query beforehand, I'd suggest a solution using mysql_fetch_field() to get metadata information: 既然您说无法指定列别名,并且无法事先知道查询的字段,所以建议使用mysql_fetch_field()获取元数据信息的解决方案:

<?php
mysql_connect('localhost', 'username', 'password');
mysql_select_db('dbname');
$result = mysql_query('select tb1.f1, tb1.f2, tb2.f1 from tb1, tb2');
for ($i = 0; $i < mysql_num_fields($result); ++$i) {
  $meta = mysql_fetch_field($result, $i);
  print_r($meta);
}

You can extract from this metadata information the table name and column name, even when there are multiple columns of the same name in the query. 您可以从此元数据信息中提取表名和列名,即使查询中有多个具有相同名称的列也是如此。

PHP's ext/mysqli supports a similar function mysqli_stmt::result_metadata() , but you said you can't know the number of fields in the query beforehand, which makes it awkward to use mysqli_stmt::bind_result() . PHP的ext / mysqli支持类似的函数mysqli_stmt::result_metadata() ,但是您说您事先不知道查询中的字段数,这使得使用mysqli_stmt::bind_result()变得很尴尬。

PDO_mysql doesn't seem to support result set metadata at this time. PDO_mysql目前似乎不支持结果集元数据。


The output from the above script is below. 上面脚本的输出如下。

stdClass Object
(
    [name] => f1
    [table] => tb1
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)
stdClass Object
(
    [name] => f2
    [table] => tb1
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)
stdClass Object
(
    [name] => f1
    [table] => tb2
    [def] => 
    [max_length] => 1
    [not_null] => 0
    [primary_key] => 0
    [multiple_key] => 0
    [unique_key] => 0
    [numeric] => 1
    [blob] => 0
    [type] => int
    [unsigned] => 0
    [zerofill] => 0
)

Prefixing field names with short version of table name is a good way to achieve it without the need to create aliases in select. 使用简短的表名前缀字段名是实现它的一种好方法,而无需在select中创建别名。 Of course you don't get the exact structure that you described but every field has its unique named index in result array. 当然,您不会获得所描述的确切结构,但是每个字段在结果数组中都有其唯一的命名索引。 For example, let's assume you have table users, and the user has a name, then call this field usr_name. 例如,假设您有表用户,并且该用户有一个名称,然后将此字段称为usr_name。

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

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