我是关于php的新手,我在mysql上获取一些数据时遇到了问题。
我需要获取存储在3个不同表中的信息,我设法获取数据,但我有一个问题,我如何获取结果。
public interface Externalizer<T> {
void write(T object, ObjectOutput out);
void read(T object, ObjectInput in);
}
class TrieExternalizer implements Externalizer<Trie> {
public void write(Trie object, ObjectOutput out) throws IOException {
out.writeUTF(object.getSomeField());
}
public void read(Trie object, ObjectInput in) throws IOException {
object.setSomeField(in.readUTF());
}
}
class Trie implements Externalizable {
private String someField;
private static final Externalizer<Trie> externalizer = new TrieExternalizer();
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
public void writeExternal(ObjectOutput out) throws IOException {
externalizer.write(this, out);
}
public void readExternal(ObjectInput in) throws IOException {
externalizer.read(this, in);
}
}
这就是我现在得到结果的方式:
$stmt = $this->conn->prepare("SELECT bike.id, bike.photo, bike.gallery, bike.motorbike_id, bike.privacy,
bike.nickname, bike.data_acquire, dets.name, dets.company, dets.release_year,
dets.displacement, dets.horsepower, dets.type, pers.id, pers.name, pers.photo,
pers.cost, pers.type, pers.date_acquire, manu.id, manu.cost, manu.name, manu.date
FROM mt_userbikes AS bike
LEFT JOIN mt_motorbikes AS dets ON dets.id = bike.motorbike_id
LEFT JOIN mt_userbike_personalizations AS pers ON pers.userbike_id = bike.id
LEFT JOIN mt_userbike_maintenances AS manu ON manu.userbike_id = bike.id
WHERE bike.id = ?");
$stmt->bind_param("i", $motorBikeId);
if ($stmt->execute()) {
$stmt->bind_result($id, $photo, $gallery, $motorbike_id, $privacy, $nickname,
$data_acquire, $motoName, $motoCompany, $motoReleaseYear,
$motoDisplacement, $motoHorsepower, $motoType, $persId,
$persName, $persPhoto, $persCost, $persType, $persDateAcquire,
$manuId, $manuCost, $manuName, $manuDate);
$data = array();
$other = array();
$i = 0;
while ($stmt->fetch()) {
if ($i === 0) {
$data["id"] = $id;
$data["photo"] = $photo;
$data["gallery"] = $gallery;
$data["motorbikeId"] = $motorbike_id;
$data["privacy"] = $privacy;
$data["nickname"] = $nickname;
$data["dataAcquire"] = $data_acquire;
$info = array();
$info["name"] = $motoName;
$info["company"] = $motoCompany;
$info["releaseYear"] = $motoReleaseYear;
$info["displacement"] = $motoDisplacement;
$info["horsepower"] = $motoHorsepower;
$info["type"] = $motoType;
$data["info"] = $info;
}
$values = array();
$values["pers.id"] = $persId;
$values["pers.name"] = $persName;
$values["pers.photo"] = $persPhoto;
$values["pers.cost"] = $persCost;
$values["pers.type"] = $persType;
$values["pers.date_acquire"] = $persDateAcquire;
$values["manu.id"] = $manuId;
$values["manu.cost"] = $manuCost;
$values["manu.name"] = $manuName;
$values["manu.date"] = $manuDate;
$other[$i] = $values;
$i++;
}
$data["other"] = $other;
我想得到什么
{
"id": 1,
"photo": null,
"gallery": null,
"motorbikeId": 1,
"privacy": null,
"nickname": "maro",
"dataAcquire": null,
"info": {
"name": "MT-07",
"company": "Yamaha",
"releaseYear": 2013,
"displacement": "689 cc",
"horsepower": "55,0 kW (74,8CV)",
"type": "Naked"
},
"other": [
{
"pers.id": 1,
"pers.name": "cupolino",
"pers.photo": null,
"pers.cost": 100,
"pers.type": "accessorio",
"pers.date_acquire": null,
"manu.id": 1,
"manu.cost": 80,
"manu.name": "controllo annuale",
"manu.date": "2015-07-28"
},
{
"pers.id": 2,
"pers.name": "copriradiatore",
"pers.photo": null,
"pers.cost": 80,
"pers.type": "accessorio estetico",
"pers.date_acquire": null,
"manu.id": 1,
"manu.cost": 80,
"manu.name": "controllo annuale",
"manu.date": "2015-07-28"
},
{
"pers.id": 3,
"pers.name": "protezioni laterale radiatore sportive",
"pers.photo": null,
"pers.cost": 100.5,
"pers.type": "accessorio estetico",
"pers.date_acquire": null,
"manu.id": 1,
"manu.cost": 80,
"manu.name": "controllo annuale",
"manu.date": "2015-07-28"
},
{
"pers.id": 4,
"pers.name": "copri avviatore",
"pers.photo": null,
"pers.cost": 15,
"pers.type": "accessorio estetito",
"pers.date_acquire": null,
"manu.id": 1,
"manu.cost": 80,
"manu.name": "controllo annuale",
"manu.date": "2015-07-28"
},
{
"pers.id": 1,
"pers.name": "cupolino",
"pers.photo": null,
"pers.cost": 100,
"pers.type": "accessorio",
"pers.date_acquire": null,
"manu.id": 2,
"manu.cost": 100,
"manu.name": "controllo 1000km",
"manu.date": "2014-09-02"
},
{
"pers.id": 2,
"pers.name": "copriradiatore",
"pers.photo": null,
"pers.cost": 80,
"pers.type": "accessorio estetico",
"pers.date_acquire": null,
"manu.id": 2,
"manu.cost": 100,
"manu.name": "controllo 1000km",
"manu.date": "2014-09-02"
},
{
"pers.id": 3,
"pers.name": "protezioni laterale radiatore sportive",
"pers.photo": null,
"pers.cost": 100.5,
"pers.type": "accessorio estetico",
"pers.date_acquire": null,
"manu.id": 2,
"manu.cost": 100,
"manu.name": "controllo 1000km",
"manu.date": "2014-09-02"
},
{
"pers.id": 4,
"pers.name": "copri avviatore",
"pers.photo": null,
"pers.cost": 15,
"pers.type": "accessorio estetito",
"pers.date_acquire": null,
"manu.id": 2,
"manu.cost": 100,
"manu.name": "controllo 1000km",
"manu.date": "2014-09-02"
}
]
}
如何区分一个表到另一个表的行? 我希望我所做的不是愚蠢的,如果可以,你能帮助我吗?
答案 0 :(得分:0)
快速完成任务
$pers = array();
$manu = array();
$i = 0;
while ($stmt->fetch()) {
if ($i === 0) {
$data["id"] = $id;
$data["photo"] = $photo;
$data["gallery"] = $gallery;
$data["motorbikeId"] = $motorbike_id;
$data["privacy"] = $privacy;
$data["nickname"] = $nickname;
$data["dataAcquire"] = $data_acquire;
$info = array();
$info["name"] = $motoName;
$info["company"] = $motoCompany;
$info["releaseYear"] = $motoReleaseYear;
$info["displacement"] = $motoDisplacement;
$info["horsepower"] = $motoHorsepower;
$info["type"] = $motoType;
$data["info"] = $info;
}
$values = array();
$values["pers.id"] = $persId;
$values["pers.name"] = $persName;
$values["pers.photo"] = $persPhoto;
$values["pers.cost"] = $persCost;
$values["pers.type"] = $persType;
$values["pers.date_acquire"] = $persDateAcquire;
$pers[] = $values;
$values = array()
$values["manu.id"] = $manuId;
$values["manu.cost"] = $manuCost;
$values["manu.name"] = $manuName;
$values["manu.date"] = $manuDate;
$manu[] = $values;
$i++;
}
$data["personalizations"] = unique($pers);
$data["maintenances"] = unique($manu)
function unnique(){ /*look on attached link*/ }
多维数组的唯一函数here