webguync Posted February 1, 2017 Share Posted February 1, 2017 $outp = ""; while($rs = $result->fetch_array(MYSQLI_ASSOC)) { if ($outp != "") {$outp .= "";} $outp .= '{"Portrait":"' . $rs["Portrait"] . '",'; $outp .= '"First Name":"' . $rs["FName"] . '",'; $outp .= '"Last Name":"' . $rs["LName"] . '",'; $outp .= '"DOI":"'. $rs["DOI"] . '",'; $outp .= '"MOI":"'. $rs["MOI"] . '",'; $outp .= '"YOI":"'. $rs["YOI"] . '",'; $outp .= '"ID":"'. $rs["id"] . '"}'; } $outp ='{"records":['.$outp.']}'; Hi for the above PHP code the output looks like this JSON format { "records": [{ "Portrait": "image1.png", "First Name": "Dave", "Last Name": "Smith", "DOI": "20", "MOI": "0", "YOI": "2017", "ID": "1" } { "Portrait": "image2.png", "First Name": "Sally", "Last Name": "Jones", "DOI": "20", "MOI": "0", "YOI": "2009", "ID": "2" }] } and this doesn't validate because there should be comma in between records like this: { "records": [{ "Portrait": "image1.png", "First Name": "Dave", "Last Name": "Smith", "DOI": "20", "MOI": "0", "YOI": "2017", "ID": "1" }, { "Portrait": "image2.png", "First Name": "Sally", "Last Name": "Jones", "DOI": "20", "MOI": "0", "YOI": "2009", "ID": "2" }] } I am trying to figure out how to get a comma added between records without also adding it at the end, b/c that is invalid also. Any ideas? Quote Link to comment Share on other sites More sharing options...
requinix Posted February 1, 2017 Share Posted February 1, 2017 Don't try to build JSON strings yourself. It's silly. If you want a JSON representation of an array (or object) then use an actual PHP array and json_encode it. $outp = ["records" => []]; while($rs = $result->fetch_array(MYSQLI_ASSOC)) { $outp["records"][] = [ "Portrait" => $rs["Portrait"], etc. ]; } echo json_encode($outp); 1 Quote Link to comment Share on other sites More sharing options...
webguync Posted February 1, 2017 Author Share Posted February 1, 2017 o.k, I will try it that way. Thx. Quote Link to comment Share on other sites More sharing options...
Barand Posted February 1, 2017 Share Posted February 1, 2017 Simpler: $outp = $result->fetch_all(MYSQLI_ASSOC); echo json_encode($outp); 1 Quote Link to comment Share on other sites More sharing options...
webguync Posted February 2, 2017 Author Share Posted February 2, 2017 Sorry to not understand fully, but what am I doing wrong here? $outp = ["records" => []]; while($rs = $result->fetch_array(MYSQLI_ASSOC)) { if ($outp != "") {$outp .= "";} $outp .= '{"Portrait":"' . $rs["Portrait"] . '",'; $outp .= '"First Name":"' . $rs["FName"] . '",'; $outp .= '"Last Name":"' . $rs["LName"] . '",'; $outp .= '"DOI":"'. $rs["DOI"] . '",'; $outp .= '"MOI":"'. $rs["MOI"] . '",'; $outp .= '"YOI":"'. $rs["YOI"] . '",'; $outp .= '"ID":"'. $rs["id"] . '"}'; } $outp ='{"records":['.$outp.']}'; $conn->close(); echo json_encode($outp); I get an error with this line $outp = ["records" => []]; Parse error: syntax error, unexpected '[' in Database-search/DB-Search.php on line 7 Quote Link to comment Share on other sites More sharing options...
benanamen Posted February 2, 2017 Share Posted February 2, 2017 (edited) What you're doing wrong is not listening to what you were already told. @Barand showed you what to do. Edited February 2, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
requinix Posted February 2, 2017 Share Posted February 2, 2017 What you're doing is completely disregarding what I and Barand said. Your database table uses columns like FName and LName. If you're okay with using those in your JSON as well then do what Barand said. If you want to keep the "First Name" and "Last Name" keys in your JSON then either do what Barand said after modifying your query to use aliases (like "FName AS `First Name`") or do what I said. Neither of those options involve building $outp as a string. Quote Link to comment Share on other sites More sharing options...
webguync Posted February 2, 2017 Author Share Posted February 2, 2017 I fully understand now and have this working as intended. Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.