Parvinder Posted January 8, 2017 Share Posted January 8, 2017 I made a mysql query in php as given below $Q = "SELECT length, width FROM statxyx WHERE siteid='$siteid'"; $R = mysqli_query($DB,$Q); //start loop //while or foreach while($row = mysqli_fetch_assoc($R)){ echo "['7C6Buh',{$row['length']},{$row['width']}],\r\n"; } The output is like ['7C6Buh',37.4192,-122.0574], ['7C6Buh',34.147,-118.1392], ['7C6Buh',23.7231,90.4086], ['7C6Buh',39.9543,-75.1657], ['7C6Buh',32.7787,-96.8217], ['7C6Buh',37.4192,-122.0574], But i want to have last row without comma. How to do this. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted January 8, 2017 Share Posted January 8, 2017 (edited) But i want to have last row without comma. How to do this. Then, don't echo it. ],\r\n should be ]\r\n. Also, look into PDO. $stmt=$db->prepare("SELECT length, width FROM statxyx WHERE siteid=?"); $stmt->execute([$siteid]); while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { echo "['7C6Buh',{$row['length']},{$row['width']}]\r\n"; } Edited January 8, 2017 by NotionCommotion Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted January 8, 2017 Solution Share Posted January 8, 2017 Put into an array then join() $Q = "SELECT length, width FROM statxyx WHERE siteid='$siteid'"; $R = mysqli_query($DB,$Q); //start loop //while or foreach $results=[]; // define array while($row = mysqli_fetch_assoc($R)){ $results[] = "['7C6Buh',{$row['length']},{$row['width']}]"; // add to array } $final = join(",\r\n", $results); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted January 8, 2017 Share Posted January 8, 2017 What is this data format? JSON? Then treat it like JSON: <?php // test data $data = [ ['7C6Buh',37.4192,-122.0574], ['7C6Buh',34.147,-118.1392], ['7C6Buh',23.7231,90.4086], ['7C6Buh',39.9543,-75.1657], ['7C6Buh',32.7787,-96.8217], ['7C6Buh',37.4192,-122.0574], ]; header('Content-Type: application/json'); echo json_encode($data, JSON_PRETTY_PRINT); The JSON_PRETTY_PRINT option automatically formats the output. However, this increases the size and doesn't make a lot of sense when the data is read by programs rather than humans. You also need to learn how to safely pass data to queries with prepared statements. 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.