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

  • Home
  • SEARCH
  • 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 5965061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:32:43+00:00 2026-05-22T19:32:43+00:00

Using Javascript I need to get some rows from a DB table, and then

  • 0

Using Javascript I need to get some rows from a DB table, and then loop through each row and use different fields from each row.

For example, if I get the rows like this:

var coordinatesArray = '<?php
    global $wpdb;
    echo $wpdb->get_var("SELECT * FROM users_coordinates WHERE lat>20 and lat<40 and long>-10 and long<40);
?>';

How should I write the following code:

// for each row do: {
//    alert the id field
//    alert the lat field
//    alert the long field
// }
  • 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-05-22T19:32:44+00:00Added an answer on May 22, 2026 at 7:32 pm

    JSON is the way to put the output in, that ensures that the result will be valid JavaScript and protects you from arbitrary code execution in JS.

    var coordinatesArray = <?php
    global $wpdb;
    // replace * by id, lat and long since you don't need other fields
    $sql = "SELECT id, lat, long FROM users_coordinates WHERE lat>20 and lat<40 and long>-10 and long<40";
    $rows = $wpdb->get_results($sql);
    // if the function fails (?), make valid JS syntax
    if (is_array($rows)) {
        echo json_encode($rows);
    } else {
        echo '[]';
    }
    ?>;
    for (var i=0; i<coordinatesArray; i++) {
        alert(coordinatesArray[i].id);
        alert(coordinatesArray[i].lat);
        alert(coordinatesArray[i]["long"]);
    }
    

    The other way would be creating a JS object with the id field as key of the JS object:

    var coordinatesMap = <?php
    global $wpdb;
    // replace * by id, lat and long since you don't need other fields
    $sql = "SELECT id, lat, long FROM users_coordinates WHERE lat>20 and lat<40 and long>-10 and long<40";
    // note: OBJECT_K, the result will be an associative array with the first field of a
    // row as key
    $rows = $wpdb->get_results($sql, OBJECT_K);
    // if the function fails (?), make valid JS syntax
    if (is_array($rows)) {
        echo json_encode($rows);
    } else {
        echo '{}';
    }
    ?>;
    for (var id in coordinatesMap) {
        if (coordinatesMap.hasOwnProperty(id)) {
            alert(id);
            alert(coordinatesMap[id].lat);
            alert(coordinatesMap[id]["long"]);
        }
    }
    

    Please replace alert by something else, it’s not really user-friendly. Remember that PHP != JavaScript and you cannot use PHP functions in JavaScript and vice versa. If you’re not sure how the output would look like, use the View source option of a page.

    References:

    • http://codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a <select>. Using JavaScript, I need to get a specific <option> from
everyone. I'm trying to get data from some page, it's updated using javascript. First,
I need to add markup to some text using JavaScript regular expressions. In Python
I am reading an xml file using javascript and then I need to submit
Is there a way to get recent articles using JavaScript from Wordpress blog? I
I'm using javascript and v3 of the maps API to pull some data from
I am trying to read text from a textarea in IE8 using some JavaScript,
I'm interested in doing some drawing using JavaScript. I'll get straight to an example:
I need to edit (using javascript) an SVG document embedded in an html page.
I need to connect to an asmx webservice using javascript. the webservice accepts and

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.