How best do I implement mysql data into a line of javascript code?

Asked
Active3 hr before
Viewed126 times

6 Answers

implement
90%

If there were 5 rows in the database there will be five lines with 5 different titles and descriptions. But I have this line of javascript that I would like to be in a while loop:,I am modifying some javascript for Google maps so that the different pin information is retrieved from my database. If I was to do this with just php it would be like this:,How can i write it so that that javascript is in a php while loop or how can i do the javascript equivelant of the php while loop so that i can retrieve multiple rows of lang,lat info each in its own javascript code like above?, Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers

ajax.php

< ? php
$obj = array();
while ($row = mysql_fetch_assoc($query)) {
   $obj[] = new array(
      "title" => $row["title"],
      "desc" => $row["desc"],
      "lat" => $row["lat"],
      "lon" => $row["lon"]
   );
}
echo json_encode($obj); ?
>

jquery ajax

$.getJSON("ajax.php", function(data) {
   for (var i = 0; i < data.length; i++) {
      map.addMarkerByLatLng(
         data[i].lon,
         data[i].lat,
         data[i].description,
         createInfo(data[i].title, ""));
   }
});
load more v
88%

Now, let’s create a file called app.js in our mysql-test directory and see how to connect to MySQL from Node.js.,Once the connection is established, we’ll use the con variable to execute a query against the database table authors:,Data returned from the MySQL database can be parsed by simply looping over the rows object.,Next, establish a connection and use the connection object to call the stored procedure as shown:

  1. Create a new project: mkdir mysql-test && cd mysql-test.
  2. Create a package.json file: npm init -y.
  3. Install the mysql module: npm install mysql.
  4. Create an app.js file and copy in the snippet below (editing the placeholders as appropriate).
  5. Run the file: node app.js. Observe a “Connected!” message.
const mysql = require('mysql');
const connection = mysql.createConnection({
   host: 'localhost',
   user: 'user',
   password: 'password',
   database: 'database name'
});
connection.connect((err) => {
   if (err) throw err;
   console.log('Connected!');
});
load more v
72%

To fill a table in MySQL, use the "INSERT INTO" statement.,To insert more than one record, make an array containing the values, and insert a question mark in the sql, which will be replaced by the value array: INSERT INTO customers (name, address) VALUES ?,The values of the properties can be displayed like this:,Fill the "customers" table with data:

Insert Multiple Records

To insert more than one record, make an array containing the values, and insert a question mark in the sql, which will be replaced by the value array:
INSERT INTO customers (name, address) VALUES ?

INSERT INTO customers(name, address) VALUES ?
65%

In the third step, we will connect to our database using the previously defined variables. We do so with the following code. The first line creates the connection to the server while the second line connects us to the correct database.,We then add the following code to let us know if the query returns no data:,The above code connects to our MySQL database, queries it, and returns data which it then exposes in a JSON format. With our data all prepared we can get onto the fun part: JS charting!,The final step in our PHP script is to close the connection we have with our server.

The first step is to create a PHP script. This can be done by using the following opening and closing tags. All code written within these tags are read by the server as PHP.

< ? php
   // code goes here.
   ?
   >
load more v
75%

In this article, my PHP, MySQL example is with database connection in xampp code.,Step 3: Create HTML form for connecting to database,Will the above code work for HTML tables. If not then pls provide a code that can save the contenteditable changes to the database, Reply 21 July 2021 HELLOWill the above code work for HTML tables. If not then pls provide a code that can save the contenteditable changes to the database


You can create your DB and table using the SQL below. You have to copy the following code and paste it into your MySQL GUI  phpmyadmin database or any other GUI or command prompt. At the bottom of the blog, you will get a git download link to download the SQL file.

--
--Database: `mydb`
   --

CREATE DATABASE IF NOT EXISTS `db_contact`
DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `db_contact`;

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

--
--Table structure
for table `tbl_contact`
   --

DROP TABLE IF EXISTS `tbl_contact`;
CREATE TABLE IF NOT EXISTS `tbl_contact`(
   `id`
   int(11) NOT NULL,
   `fldName`
   int(50) NOT NULL,
   `fldEmail`
   int(150) NOT NULL,
   `fldPhone`
   varchar(15) NOT NULL,
   `fldMessage`
   text NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = latin1;

--
--Indexes
for dumped tables
--

--
--Indexes
for table `tbl_contact`
   --
ALTER TABLE `tbl_contact`
ADD PRIMARY KEY(`id`);

--
--AUTO_INCREMENT
for dumped tables
--

--
--AUTO_INCREMENT
for table `tbl_contact`
   --
ALTER TABLE `tbl_contact`
MODIFY `id`
int(11) NOT NULL AUTO_INCREMENT;
load more v
40%

Use MySQL Shell to execute the content of the file code.sql as SQL. , Use MySQL Shell to execute the content of the file code.py as Python code. , Use MySQL Shell to execute the content of the file code.js as JavaScript code. , MySQL Shell can execute SQL, JavaScript or Python code, but only one language can be active at a time. The active mode determines how the executed statements are processed:

Use MySQL Shell to execute the content of the file code.sql as SQL.

$ > mysqlsh--sql < code.sql
load more v

Other "implement-undefined" queries related to "How best do I implement mysql data into a line of javascript code?"