Jump to content

How to remove comma from last row in mysql query in php


Parvinder
Go to solution Solved by Barand,

Recommended Posts

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.
Link to comment
Share on other sites

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 by NotionCommotion
Link to comment
Share on other sites

  • Solution

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);
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.