Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8691293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:05:44+00:00 2026-06-13T00:05:44+00:00

I have a MySQL query like this written in PHP: $merkki = $_GET[merkki]; //

  • 0

I have a MySQL query like this written in PHP:

$merkki = $_GET["merkki"];
// Retrieve all the data from the table 
//to show models based on selection of manufacturer

$result = mysql_query("SELECT * FROM Control_Mallit WHERE merkki_id = $merkki")
or die(mysql_error()); 
echo '{';
while($row = mysql_fetch_array($result)){
    echo '"' . $row['id'] . '"' . ":" . '"' . $row['malli'] . '"';
}
echo '}';

Result is correct, but how I can get a comma after each record? If I echo (,) after each row my code doesn’t work. I need it formatted as described below.

{
    "":"--",
    "series-1":"1 series",
    "series-3":"3 series",
    "series-5":"5 series",
    "series-6":"6 series",
    "series-7":"7 series"
}

What’s the best way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:05:47+00:00Added an answer on June 13, 2026 at 12:05 am

    Immediately stop using your code. It is vulnerable to SQL injection. Think of what would happen if the value of merkki was 1 OR 1=1. The statement would return all records:

    SELECT * FROM Control_Mallit WHERE merkki_id = 1 OR 1=1
    

    You need to bind parameters to your query using mysqli_ or PDO functions (mysql_ functions are being deprecated. Also, use a column list and do not SELECT *.

    Here is a possible solution using mysqli_ (not tested):

    <?php
    $link = mysqli_connect('localhost', 'my_user', 'my_password', 'world');
    
    /* check connection */
    if (!$link) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    $array = array();
    
    /* create a prepared statement */
    $stmt = mysqli_prepare($link, "SELECT id, malli FROM Control_Mallit WHERE merkki_id = ?");
    
    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, 'i', $_GET[merkki]);
    
    /* execute query */
    $result = mysqli_stmt_execute($stmt);
    
    /* fetch associative array */
    while ($row = mysqli_fetch_assoc($result)) {
        $array[] = '"' . $row['id'] . '"' . ":" . '"' . $row['malli'] . '"';
    }
    
    /* free result set */
    mysqli_free_result($result);
    
    /* close connection */
    mysqli_close($link);
    
    echo '{' . implode(',', $array) . '}';
    
    ?>
    

    Edit

    Following your original code, this solution should work:

    $merkki = $_GET["merkki"];
    $array = array();
    // Retrieve all the data from the table to show models based on selection of manufacturer
    $result = mysql_query("SELECT * FROM Control_Mallit WHERE merkki_id = $merkki")
    or die(mysql_error()); 
    while($row = mysql_fetch_array($result)){
        $array[] = '"' . $row['id'] . '"' . ":" . '"' . $row['malli'] . '"';
    }
    echo '{' . implode(',', $array) . '}';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql query like this: $topicreplycount = mysql_query('SELECT COUNT(DISTINCT post_msg_id) FROM phpbb_attachments
I need to have MySQL query like this one: UPDATE table_name SET 1 =
I have a MySQL query like this: SELECT DAYNAME(CreatedAt) AS TheDay, SUM(Amount) AS TheSum
I have a mysql query Select * from tbl_schoolphotos where Filename like 'glasshouse_1_%' order
I have the following mysql query which I am running with php like so.
I have a webapp written in PHP using a MySQL database backend. This question
I have written something like this in PHP <script type=text/javascript src=jquery.js></script></script> <script type=text/javascript> function
I have a MySQL query like this: SELECT *, SUM(...some SQL removed for brevety)
I have some code that written in php to use for export data from
I have a MySQL query that I would like to optimize a little bit.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.