将JavaScript连接到MySQL数据库

时间:2017-04-24 11:58:20

标签: javascript php mysql

我正在为电影院开发一个网站。我一直在使用Axure来设计网站。我使用相同的软件创建了一个座位预订网页。该页面有一个很长的js代码。我可以使用PHP将此页面连接到数据库吗?对于HTML页面,我做了类似的事情(使用PHP):

package com.first.majazen.appfinal;


import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Set;
import java.util.UUID;
import android.os.Handler;


public class Recieve extends Activity {
    private static String mystuff;
    private static final String TAG = mystuff;
    BluetoothSocket mmSocket = null;
    BluetoothDevice mmDevice = null;
    BluetoothAdapter mBluetoothAdapter;
    DataInputStream mmInputStream;
    Thread workerThread;
    byte[] readBuffer;
    int readBufferPosition;
    volatile  boolean stopWorker;
    EditText text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recieve);

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        text = (EditText)findViewById(R.id.text);
        if (!mBluetoothAdapter.isEnabled()) {
            Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBluetooth, 0);
            Log.i("myStuff", "Bluetooth Enabled");
        } else {
            Log.i("myStuff", "Bluetooth Already Enabled");
        }
        Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                if (device.getName().equals("RNBT-E965")) {
                    mmDevice = device;
                    Log.i("myStuff", "Device equals " + device.getName());
                    try {
                        openBT();
                    } catch (IOException e) {
                        Log.i("myStuff", "BT not opened");
                    }
                }}
        }




    }

    public void openBT()throws IOException {


        UUID uuid = mmDevice.getUuids()[0].getUuid(); //if you don't know the UUID of the bluetooth device service, you can get it like this from android cache
        Log.d(TAG, "UUID: " + uuid.toString());
        mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
        mmSocket.connect();
        Log.i("myStuff", "Connected OK!");
        mmInputStream = new DataInputStream(mmSocket.getInputStream());
        beginListenForData()
    }


    public void BackActivity(View view) {
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }


   public void beginListenForData() {
      final Handler handler = new Handler();
        final byte delimiter = 10;

       stopWorker = false;
        readBufferPosition =0;
        readBuffer = new byte[1024];
        workerThread = new Thread(new Runnable() {

            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()&& !stopWorker){
                    try{
                        text.setText("manal");

                        int bytesAvailable = mmInputStream.available();
                        if(bytesAvailable >0){
                            byte[] packetBytes =new byte[bytesAvailable];
                            mmInputStream.read(packetBytes);
                            for (int i=0; i<bytesAvailable; i++){
                                byte b = packetBytes[i];
                                if (b== delimiter){
                                    byte[] encodedBytes = new byte[readBufferPosition];
                                    System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
                                    final String data = new String(encodedBytes, "US-ASCII");
                                    readBufferPosition = 0;
                                    handler.post(new Runnable() {
                                        public void run(){
                                            text.setText(data);

                                            Log.i("myStuff", "Received");

                                        }
                                    });
                                } else {
                                    readBuffer[readBufferPosition++] = b;
                                }
                            }
                        }
                    }catch (IOException ex){
                        stopWorker = true;
                        Log.i("myStuff", "not Received");

                    }
            }
        }
        }); workerThread.start();
    }




}

有没有办法为.js文件做同样的事情?

1 个答案:

答案 0 :(得分:0)

您无法将JS直接连接到您的数据库,但您可以创建将执行该连接并获取所需数据的PHP代码。您可以通过Ajax调用从JavaScript代码调用该PHP脚本。

PHP可以从数据库数据中生成JSON数组,因此您可以轻松地在JS端解析它。