我有一個小問題,以在PHP代碼如何獲得此結果與從MySQL的MySQL,PHP,JSON追加從MySQL數據轉換成在PHP一個陣列和JSON文件轉換
提供{"profiles":[
{"ID": "39780b57-9181-4a41-a31e-5d4b3fa59a50", "Name": "Mihai - BP Dev Team","CountryCode": "ro","PictureID": "a30d750a-38e6-407f-a722-943fe3711807","IsStandard": true,"IsOnline": true,"IsPremium": true,"IsVerified": true,"Age": 27,"CityStateCode": "Bucharest"},
{"ID": "e1dd5bab-1eeb-4729-a4f6-0baeb851f750", "Name": "Nicolai", "CountryCode": "dk", "PictureID": "af1345b5-8380-4300-abf0-1d5f15c90040", "IsStandard": true, "Age": 32, "CityStateCode": "Valby"},
{"ID": "2c8535ec-25a4-4a3e-a333-c3797aff491f", "Name": "Testing", "CountryCode": "ca", "PictureID": "ba44bf9b-1592-40a8-b60c-068603bfb9c1", "IsStandard": true, "Age": 30, "CityStateCode": "Centre-Sud North"}}
]}
這是數據我的PHP代碼:
$accessDb = new Connexion("gaiurba_MyBD");
$connexion2 = $accessDb->openConnexion();
if ($query2 = $connexion2->prepare("SELECT u.UserName as UserName, u.PictureID as PictureID, u.CountryCode as CountryCode,
u.DateOfBirth as DateOfBirth, u.CityCode as CityCode, u.VerifiedTimestamp as VerifiedTimestamp,
u.OnlineStatus as OnlineStatus, u.IsPaying as IsPaying, u.StatusAvailableForDates as StatusAvailableForDates,
cc.$languageCode as CityStateCode, os.$languageCode as LabelOnlineStatus
FROM Users u
INNER JOIN CityCode cc ON u.CityCode = cc.id
INNER JOIN OnlineStatus os ON u.OnlineStatus = os.id
WHERE u.ID = ?")) {
$query2->bind_param('s', $id);
$query2->execute();
$query2->bind_result($UserName, $PictureID, $CountryCode, $DateOfBirth, $CityStateCode, $VerifiedTimestamp, $OnlineStatus, $IsPaying, $StatusAvailableForDates, $CityStateCode, $LabelOnlineStatus);
$query2->store_result();
$affected2 = $connexion2->affected_rows;
// TRACE
echo "[affected2 row 2 = $affected2]";
if ($affected2 == 1) {
while ($query2->fetch()) {
echo "[ID = $id - UserName = $UserName - PictureID = $PictureID - CountryCode = $CountryCode - DateOfBirth = $DateOfBirth - CityStateCode = $CityStateCode - VerifiedTimestamp = $VerifiedTimestamp - OnlineStatus = $OnlineStatus - IsPaying = $IsPaying - StatusAvailableForDates = $StatusAvailableForDates - CityStateCode = $CityStateCode - LabelOnlineStatus = $LabelOnlineStatus]";
}
} else {
$result = '{"exception":false,"success":false,"status":0,"message":"ERROR SQL Query : SELECT-ULL-ID Number of row !","confirmMessage":null,"html":null,"data":null}';
}
} else {
$result = '{"exception":false,"success":false,"status":0,"message":"ERROR SQL Query : SELECT-ULL-ID Number of row !","confirmMessage":null,"html":null,"data":null}';
}
我知道正確的代碼是關於我的,而($ query2->取())的情形,因爲我的回聲看到我的BD讀取我的好信息。首先,我需要在PHP中創建一個數組,並追加從BD和最終用途
echo json_encode($result);
獲取的每一行不要被我注意我的數據返回選擇和JSON字符串的數據。我知道我將不得不在我的while循環中格式化一些數據以獲得正確的結果。
我只需要知道如何創建一個數組,其中我爲MySQL返回的每一行添加一個新數組。
謝謝你的幫忙!
非常感謝您的解釋!在NetBeans中,我有一行語法錯誤:$ profiles []:= $ profile;我使用你的聲明$ profile = new StdClass();和$ profiles = new StdClass(); – LiTHiUM2525
'$ profiles'不應該是一個對象,而是一個數組。 '新的StdClass()'應該可以正常工作,但我認爲括號是可選的,因爲沒有參數給構造函數。另請參見:[PHP和StdClass中的動態屬性](http://krisjordan.com/dynamic-properties-in-php-with-stdclass) – GolezTrol