我正在尝试从服务器端mysql数据创建一个websql表。 下面有效,但我想知道有更好的方法吗?
PHP脚本,其中包含javascript。
<?php
include_once("../../inc/config.inc.php");
//access_control();
$query = "SELECT id, productname, price FROM `products`";
$result = $mysqli->query($query) or die('error');
?>
<script>
var db = window.openDatabase("test", "1.0", "test local", 2 * 1024 * 1024);
window.onload = function() {
if (!window.openDatabase) {
status("Database not supported");
return;
}
db.transaction(function(tx) {
//tx.executeSql("drop table if exists products");
tx.executeSql("create table if not exists products ("
+ "id integer primary key,"
+ "name text,"
+ "price integer"
+ ")",null,null, logger);
<php?
$i=1;
if($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "tx.executeSql(\"INSERT INTO products(id, name, price) VALUES ('".$row['id']."','".$row['productname']."','".$row['price']."');\");\n";
}
}
?>
});
db.transaction();
</script>
php脚本的输出只提供javascript。
<script>
var db = window.openDatabase("test", "1.0", "test local", 2 * 1024 * 1024);
window.onload = function() {
if (!window.openDatabase) {
status("Database not supported");
return;
}
db.transaction(function(tx) {
tx.executeSql("drop table if exists products");
tx.executeSql("create table if not exists products ("
+ "id integer primary key,"
+ "name text,"
+ "price integer"
+ ")",null,null, logger);
tx.executeSql("INSERT INTO products(id, name, price) VALUES ('12','bla bla ','3.99');");
tx.executeSql("INSERT INTO products(id, name, price) VALUES ('13','BO BO','3.40');");
});
db.transaction();
</script>