我的将 JSON 发送到 mysql 的代码有什么错误?
我的 php 函数从 twitter 获取推文将它们作为 json 编码字符串返回。 JS 脚本捕获该数据并进行处理。使用 ajax 将其 POST 到 php 脚本。接收php脚本解码json并将其插入表中。但是mysql没有数据。错误控制台显示没有错误。这是我的代码:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<?php
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return json_encode($xml);
;
} //--------------- end of function
?>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$(document).ready(function()
{
var msg_top = new Array();
msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
var url = "msg2_mysql.php"
var request = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
}
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
if (request.readyState == 4) {
if (request.status == 200) {
alert('POST');
} else {
alert(request.status); //
}
}
}
request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
});
</script>
</body>
</html>
这是我用来将数据插入表的脚本
<?php
$username = "******";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect
to MySQL");
$selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
if($_POST['msg_top']!="")
{
$json = $_POST['msg_top'];
$msg = strtoupper(json_decode($json));
$query = "INSERT INTO msg2 (id,msg,msg_id,depth) VALUES ('','$msg','ID','3')";
mysql_query($query);
if(!mysql_query($query, $dbh))
{die('error:' .mysql_error());} echo'success';
}
else echo('no value!');
?>
My php function fetches tweets from twitter & return them as json encoded string. A JS script catches that data & POST it to a php script using ajax. The receiving php script decodes json and insert it into table. But there is no data into mysql. Error console shows no error. Here is my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<?php
function searchResults($q) {
$host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//Raw xml
$result = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($result);
return json_encode($xml);
;
} //--------------- end of function
?>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<script>
$(document).ready(function()
{
var msg_top = new Array();
msg_top = "<"+"?php echo searchResults('windows');"+"?"+">";
var url = "msg2_mysql.php"
var request = null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
request=new XMLHttpRequest();
}
else
{// code for IE6, IE5
request=new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("POST", url, true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.setRequestHeader("Connection", "close");
request.onreadystatechange = function(){
if (request.readyState == 4) {
if (request.status == 200) {
alert('POST');
} else {
alert(request.status); //
}
}
}
request.send("msg_top=" + encodeURIComponent(msg_top).replace(/%20/g, '+'));
});
</script>
</body>
</html>
Here is script I am using to insert data into table
<?php
$username = "******";
$password = "********";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect
to MySQL");
$selected = mysql_select_db("spec",$dbh) or die("Could not select first_test");
if($_POST['msg_top']!="")
{
$json = $_POST['msg_top'];
$msg = strtoupper(json_decode($json));
$query = "INSERT INTO msg2 (id,msg,msg_id,depth) VALUES ('','$msg','ID','3')";
mysql_query($query);
if(!mysql_query($query, $dbh))
{die('error:' .mysql_error());} echo'success';
}
else echo('no value!');
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些行是错误的:
首先,它是“Array”,而不是“array”。实际上您需要的是:
问题是如何将 php 域中的数组编码为 Javascript 数组常量。我怀疑使用 JSON 编码器是最简单的事情。
编辑 - 我会重复警告,我不懂 php,但你的代码可能如下所示:
These lines are wrong:
First, it's "Array", not "array". Really all you need is:
The problem is going to be how to encode the array from the php domain into a Javascript array constant. I suspect that using a JSON encoder would be the simplest thing to do.
edit — I'll repeat the caveat that I don't know php, but your code might look like this: