我有一个具有垂直模式的数据库,解释如下:
Key、Cat、UID、Var、Val等其中Key是索引,Cat是类别,UID是一组行的标识符,var是该行表示的值的名称,Val是该值。所以有几行看起来像这样:
key cat uid var val
1 1 98765 name David
2 1 98765 description handsome, young, sporting,
3 1 98765 phone 123-456-789
4 1 98765 email david@handsome.com
5 1 12345 name Jason
6 1 12345 description cool, hot, tall,
7 1 12345 phone 222-555-1244
8 1 12345 email jason@cool.org
8 1 12345 website www.jason.com因此,查询/php的目标是获取指定类别中的所有行,然后根据具有该uid的所有行的uid返回一个关联数组。结果会是这样的
results{
["98765"] => ["name"] = David
=> ["description"] = handsome, young, sporting,
=> ["phone"] = 123-456-789
=> ["email"] = david@handsome.com
["12345"] => ["name"] = Jason
=> ["description"] = cool, hot, tall,
=> ["phone"] = 222-555-1244
=> ["email"] = jason@cool.org
=> ["website"] = www.jason.com
}希望这是可以理解的,如果我的问题不清楚,请让我知道。
发布于 2011-11-18 06:55:34
尝试如下所示:
$category = 1;
$result = mysql_query( 'SELECT * FROM table WHERE cat = ' . intval( $category));
$results = array();
while( $row = mysql_fetch_array( $result))
{
$results[ $row['uid'] ][ $row['var'] ] = $row['val'];
}
mysql_free_result( $result);发布于 2011-11-18 07:05:13
如果这样做的原因是为了使用result"98765“类型语法访问结果,那么使用对象可能更好。您可以引用对象或使用对象数组,但代码看起来要干净得多
https://stackoverflow.com/questions/8175542
复制相似问题