我有一个垂直模式的数据库,解释说:如何将具有匹配列值的MySQL查询结果放入基于PHP值的关联数组中?
重点,猫,UID,VAR,缬氨酸等.. ,其中关键的是指数,猫类,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 [email protected]
5 1 12345 name Jason
6 1 12345 description cool, hot, tall,
7 1 12345 phone 222-555-1244
8 1 12345 email [email protected]
8 1 12345 website www.jason.com
因此与查询/ PHP的目的是为了得到一个指定类别的所有行,然后返回基于UID的关联数组用于所有的行UID。结果会像
results{
["98765"] => ["name"] = David
=> ["description"] = handsome, young, sporting,
=> ["phone"] = 123-456-789
=> ["email"] = [email protected]
["12345"] => ["name"] = Jason
=> ["description"] = cool, hot, tall,
=> ["phone"] = 222-555-1244
=> ["email"] = [email protected]
=> ["website"] = www.jason.com
}
希望这是可以理解的,让我知道如果我的问题不清楚。
工作得很好,谢谢。这是完成这个最有效的方法吗?或者会使用像fetchAll()更好? – Kawika
你需要MySQLi来使用'fetch_all()',效率差异可能是微不足道的,因为你仍然需要遍历整个结果集来正确地形成你的数组。 – nickb
很酷,很高兴知道。谢谢! – Kawika