簡體   English   中英

HTML、CSS、JS 和數據庫 - 我如何連接所有 4 個?

[英]HTML, CSS, JS & DataBase - how can i connect all 4?

我在將任何數據庫連接到我的 HTML CSS JS 項目時遇到問題。 我閱讀和厭倦了很多東西,我只是迷路了。 連接適用於 HTML 和 JS 的任何數據庫的最簡單方法是什么。

我試了SQLite,還是不行,請問怎么解決? 提前致謝,奧馬爾。

你可以試試這個

https://pouchdb.com/

var db = new PouchDB('dbname');

db.put({
  _id: 'dave@gmail.com',
  name: 'David',
  age: 69
});

db.changes().on('change', function() {
  console.log('Ch-Ch-Changes');
});

db.replicate.to('http://example.com/mydb');

或者它可以用於 firebase 和 firebase SDK

https://firebase.google.com/docs/storage/web/start#web-version-9

import { initializeApp } from "firebase/app";
import { getStorage } from "firebase/storage";

// TODO: Replace the following with your app's Firebase project configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
  storageBucket: ''
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);


// Initialize Cloud Storage and get a reference to the service
const storage = getStorage(app);

要從 JavaScript 連接到 SQLite 數據庫,您需要使用服務器端語言,如 PHP 或 Node.js 作為中介。

您可以使用 php sqlite 連接數據庫

<?php
// Connect to the database
$db = new SQLite3("mydatabase.db");

// Perform a SELECT query
$result = $db->query("SELECT * FROM users");

// Print the results as a JSON array
$rows = array();
while ($row = $result->fetchArray()) {
    $rows[] = $row;
}
echo json_encode($rows);

// Close the connection
$db->close();
?>

JavaScript 向服務器發送 AJAX 請求並處理響應的代碼:

function fetchUsers() {
    // Send an AJAX request to the server
    const xhr = new XMLHttpRequest();
    xhr.open("GET", "get_users.php");
    xhr.onload = function() {
        if (xhr.status === 200) {
            // Update the page with the results
            let users = JSON.parse(xhr.responseText);
            for (let i = 0; i < users.length; i++) {
                let user = users[i];
                let name = user["name"];
                let email = user["email"];
                // ...
            }
        } else {
            console.error(xhr.statusText);
        }
    };
    xhr.onerror = function(error) {
        console.error(error);
    };
    xhr.send();
}
```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM