php / javascript无法在线工作

时间:2015-09-27 20:04:17

标签: javascript php mysql

我在php和javascript中有一些代码可以在我的计算机上运行localhost,但是不能在线工作。我用Godaddy打电话给他们。他们说一切都很好看。他们说他们无法解决编码问题。我有一个包含一些引号的数据库。我正在尝试执行的功能是从数据库中获取随机引用并将其显示在我的网页上,然后每10秒将其更改为一个新的随机引用。我对php或javascript一无所知。我付了别人写这段代码,他们说代码很好。

javascript是:

var qouteobj=createRequestObject();
function getqoute(){
qouteobj.open("GET","php/randomquote.php",true);
qouteobj.send(null);
qouteobj.onreadystatechange=function(){
if(qouteobj.readyState==4 && qouteobj.status==200){
var q_rec=qouteobj.responseXML;
var rq=q_rec.getElementsByTagName('qoute');
var txt="";
var i;
for (i=0;i<rq.length;i++){
txt=txt + rq[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById('random-quote').innerHTML=txt;
setTimeout('getqoute()',10000);
setTimeout('getqoute()',10000);
}
}

randomquote.php文件是:

<?xml version='1.0' ?>
<?php
include "connect.php";
header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );

$sql = "SELECT quote FROM quotes where Rand() Limit 1";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
$xml="<root>";
$xml.="<qoute>".$row['quote']."</qoute>";

$xml.="</root>";
echo $xml;
//echo $row['quote'];
mysqli_close($conn);
?>

html很简单:

<p id="random-quote"></p>

我知道它正在连接数据库,因为我在网站上有其他工作的PHP代码,从数据库中提取数据并在页面上显示。该页面位于:www.bestmoviequote.com感谢您为我调查此内容。

2 个答案:

答案 0 :(得分:1)

我想问题来自你在发送标题之前写东西的事实。

尝试:

<?php
    include "connect.php";

    $sql = "SELECT quote FROM quotes where Rand() Limit 1";
    $result = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($result);
    mysqli_close($conn);

header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
$xml="<?xml version='1.0' ?>";
$xml.="<root>";
$xml.="<qoute>".$row['quote']."</qoute>";
$xml.="</root>";
echo $xml;

如果仍有问题,请在包含连接脚本之前添加ini_set('display_errors', 1),并向我们提供错误

答案 1 :(得分:0)

我们得到了它的工作。我们还遇到了原始代码没有显示真正随机引用的问题。它倾向于使用更多喜欢的引号(我有另一个页面,用户可以喜欢这些引号。)以下解决方案解决了这两个问题。

使用Javascript:

var qouteobj=createRequestObject();
function getqoute(){
qouteobj.open("GET","/php/randomquote.php",true);
qouteobj.send(null);
qouteobj.onreadystatechange=function(){
if(qouteobj.readyState==4 && qouteobj.status==200)
{
var q_rec=qouteobj.responseText;
if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(q_rec,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(q_rec);
}
var rq=xmlDoc.getElementsByTagName("qoute");
var txt="";
var i;
for (i=0;i<rq.length;i++){
txt=txt + rq[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById('random-quote').innerHTML=txt;
setTimeout('getqoute()',10000);
}
}
}

randomquote.php:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "moviequotes";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
header("Content-Type: text/xml; charset=utf-8");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
$sql = "SELECT COUNT(*) FROM quotes";
$result = mysqli_query($conn,$sql);
$rows=$result->fetch_row();
//$rows = mysqli_num_rows($result);
$rndm=rand(1,$rows[0]);
$sql = "SELECT quote FROM quotes where id='".$rndm."' Limit 1";
$result = mysqli_query($conn,$sql);
if($result)
{
$row = mysqli_fetch_assoc($result);
$xml='<root>';
$xml.='<qoute>'.htmlspecialchars($row['quote']).'</qoute>';
$xml.='</root>';
echo $xml;
}
else
{
echo "Not working";
}
mysqli_close($conn);
?>