我有一个垂直模式的数据库,说明:
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
因此,query/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
}
希望你能理解,如果我的问题不清楚,请告诉我。
试试这样:
$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);
如果这样做的原因是使用result["98765"]类型语法访问结果,那么最好使用对象。你可以引用对象或者使用对象数组但是代码会更简洁