gpt4 book ai didi

php - 如何使用 appcelerator titanium 和 php 将数据从 sqlite 数据库插入远程 mysql 数据库

转载 作者:行者123 更新时间:2023-11-29 06:51:52 24 4
gpt4 key购买 nike

我试过使用下面的代码。但它不起作用。我有一个临时的 sqllite 表,我需要将所有数据从临时数据库插入到远程 mysql 服务器。

var url = "http://bmcagro.com/manoj/insertopinion.php";
var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
// this.responseText holds the raw text return of the message (used for JSON)
// this.responseXML holds any returned XML (used for SOAP web services)
// this.responseData holds any returned binary data
Ti.API.debug(this.responseText);
var json = this.responseText;
var response = JSON.parse(json);

if (response.logged == "true") {
var newtoast = Titanium.UI.createNotification({
duration: 1000,
message: "Inserted"
});
newtoast.show();
} else {
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "False"
});
toast.show();
}
},
onerror: function(e) {
Ti.API.debug(e.error);
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "Error in Connection!!"
});
toast.show();
},
timeout:5000 });

xhr.open("POST", url);
xhr.send({names: names});
});

php代码是

<?php
$con = mysql_connect("MysqlSample.db.8189976.hostedresource.com","MysqlSample","xszZ@123ddlj");
if (!$con) {
echo "Failed to make connection.";
exit;
}
$db = mysql_select_db("MysqlSample",$con);
if (!$db) {
echo "Failed to select db.";
exit;
}
$names = $_POST['names'];
foreach ($names as $name) {
mysql_query("INSERT INTO seekopinion(uid,gid,opiniondescription,date,postedto) VALUES (" + $name.gid + "," + $name.tempid + "," + $name.gid + ",NOW()," + $name.gid + ")");
}
if($query) {
$sql = "SELECT * FROM MysqlSample.seekopinion";
$q= mysql_query($sql);
$row = mysql_fetch_array($q);
$response = array(
'logged' => true,
'seekopinion' => $row['seekopinion']
);
echo json_encode($response);
} else {
$response = array(
'logged' => false,
'message' => 'User with same name exists!!'
);
echo json_encode($response);
}
?>

实际上我是 php 和 titanium 的初学者...任何人都可以帮助我。

最佳答案

终于找到出路了....我在 appcelerator 中使用定界符“-”将整行更改为字符串,然后将参数传递给 php 代码...从使用 explode 拆分代码的位置,然后使用 for 循环插入

用于将表从 sqllite 数据库发布到 mysql 数据库的 appcelerator 代码..

postbutton.addEventListener('click', function(e) 
{
var names = [];
var datarow ="";
var db = Ti.Database.open('weather');
var rows = db.execute('SELECT tempid,gid,name,email FROM postedto');
while (rows.isValidRow())
{
datarow=datarow+"-"+rows.fieldByName('tempid')
rows.next();
}
db.close();
var params = {
"uid": Ti.App.userid,
"opiniondescription": question2.text,
"database": datarow.toString()
};

var url = "http://asdf.com/as/asd.php";
var xhr = Ti.Network.createHTTPClient({
onload: function(e) {
// this.responseText holds the raw text return of the message (used for JSON)
// this.responseXML holds any returned XML (used for SOAP web services)
// this.responseData holds any returned binary data
Ti.API.debug(this.responseText);
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged ==true)
{
var seekopinion11=require('seekopinion2');
var seekop11 = new seekopinion11();
var newWindow = Ti.UI.createWindow({
//fullscreen : true,
backgroundImage : 'images/background.jpg',
});
newWindow.add(seekop11);
newWindow.open({
//animated : true
});
}
else
{
var toast = Titanium.UI.createNotification({
duration: 2000,
message: response.message
});
toast.show();
}
},
onerror: function(e) {
Ti.API.debug("STATUS: " + this.status);
Ti.API.debug("TEXT: " + this.responseText);
Ti.API.debug("ERROR: " + e.error);
var toast = Titanium.UI.createNotification({
duration: 2000,
message: "There was an error retrieving data.Please try again"
});
toast.show();
},
timeout:5000
});

xhr.open("GET", url);
xhr.send(params);
});

使用explode分解字符串的php代码

  <?php  
$con = mysql_connect("MysqlSample.db.hostedresource.com","MysqlSample","xszZ@");
if (!$con)
{
echo "Failed to make connection.";
exit;
}
$db = mysql_select_db("MysqlSample",$con);
if (!$db)
{
echo "Failed to select db.";
exit;
}
$uid= $_GET['uid'];
$opiniondescription= $_GET['opiniondescription'];
$database= $_GET['database'];
$insert = "INSERT INTO seekopinion(uid,opiniondescription,date) VALUES ('$uid','$opiniondescription',NOW())";
$query= mysql_query($insert);
$rows = explode("-", $database);
$arrlength=count($rows);
for($x=0;$x<$arrlength;$x++)
{
$insert = "INSERT INTO seekopinionuser(sid,postedto) VALUES ((SELECT MAX(sid) FROM seekopinion),$rows[$x])";
$query= mysql_query($insert);
}
if($query)
{
$sql = "SELECT s.sid,s.opiniondescription,s.uid,u.postedto FROM seekopinion s left join seekopinionuser u on s.sid=u.sid WHERE uid=$uid AND s.sid=(SELECT MAX(sid) FROM seekopinion) ";
$q= mysql_query($sql);
$row = mysql_fetch_array($q);
$response = array(
'logged' => true,
'opiniondescription' => $row['opiniondescription'],
'uid' => $row['uid'] ,
'sid'=>$row['sid']

);
echo json_encode($response);

}
else
{
$response = array(
'logged' => false,
'message' => 'Seek opinion insertion failed!!'
);
echo json_encode($response);
}
?>

关于php - 如何使用 appcelerator titanium 和 php 将数据从 sqlite 数据库插入远程 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14869867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com