1

我正在尝试使用 php 和 java 脚本显示来自 mysql 的数据。我设法在页面上显示数据,但无法将其发送到脚本。以下是我的文件

script1.js

$(document).ready( function() {
 done();
});

function done() {
      setTimeout( function() { 
      updates(); 
      done();
      }, 200);
}


function updates() {

  $.getJSON("cocktail.php", function(data){
      $("ul") .empty ();

      $each(data.result, function(){
          $("ul").append("<li>Poza: "+this['poza']+"</li> <li>Nume: "+this['nume']+"</li><li>Compozitie: "+this['compozitie']+"</li><br/>");

  });
   }); 
}

鸡尾酒.php

<?php include ('includes/header_js.php');?>
<?php include_once ('includes/connection.php');?>

<div class="body_bg">
                        <h2>Arta Cocktail-urilor</h2>


                        <div class="clr"> </div>

<?php

$sql = "SELECT * FROM   cocktail";
$res = mysql_query($sql);
$result = array ();

while($row = mysql_fetch_array ($res) )
{

array_push($result, array('poza' => $row[1],
                            'nume' => $row[2],
                            'compozitie' =>$row[3]));
}
echo json_encode(array("result" => $result));

?>

 </div><!--end of body_bg-->

<?php include ('includes/footer.php'); ?>

连接.php

<?php 
$connection = mysql_connect('localhost', 'root'. '');
if(!$connection){
die('Nu s-a putut conecta la baza de date.' .mysql_error());    
}
$db_select = mysql_select_db('first_class', $connection);
if(!$db_select){
die('Eroare de conexiune'.mysql_error());   
}

?>

头文件.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>First Class Cocktail</title>
<link  rel="stylesheet" href="stylesheets/style.css"  type="text/css" />
</head>
<body>
<table></table>
<script type="text/javascript" src="javascripts/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="javascripts/script1.js"></script>
<div class="container">
<div class="header">




                </div><!--end of logo-->
        <div class="menu">
         <ul>
          <li><a href="index.php" class="active"><span>Acasa </span></a></li>
          <li><a href="galerie.php"><span>Galerie</span></a></li>
          <li><a href="cocktail.php"><span>Cocktail</span></a></li>
          <li><a href="about.php"><span> Despre Noi </span></a></li>
          <li><a href="contact.php"><span> Contact </span></a></li>
        </ul>

        </div><!--end of menu-->
        <div class="clr"></div><!--end of clr-->

我哪里失败了???提前感谢您浪费时间阅读本文!谢谢!

4

2 回答 2

1

You're generating a multi-level array:

array_push($result, array('poza' => $row[1], etc...);

will create an aray like:

$result = array(
     0 => array('poza' => ....)
);

meaning you have to use

      $("ul").append("<li>Poza: "+this[0]['poza']+ etc...);
                                      ^^^---- note this

in your JS code.

Your PHP-side code can be tremendously simplified:

SELECT poza, nume, compozitie FROM ...

$data = array();
while ($row = mysql_fetch_assoc($result)) {
   $data[] = $row;
};

As well, your JS code seems to be assuming there will only ever be ONE row of data coming out of the database, yet your database handling code is set up to handle MULTIPLE rows. Not my place to figure out which one is correct, but you should be aware of it.

于 2013-10-15T16:23:11.300 回答
1

问题在于它cocktail.php包含 HTML,而$.getJSON函数需要 JSON(和仅 JSON)。您还应该Content-Type为 JSON 返回一个有效的标头,例如application/json. 最后你cocktail.php会看起来像这样:

<? 

include_once('includes/connection.php');

header('Content-Type: application/json');

$sql = 'SELECT poza, nume, compozitie FROM cocktail';
$res = mysql_query($sql);
$results = array();

while( $row = mysql_fetch_assoc($res) ) {

    $results[] = $row;

}

echo json_encode( array('results' => $results) );

?>

在旁注中,我看到您正在使用这些mysql_...功能。这些函数在较新版本的 PHP 中已被弃用,强烈建议使用较新的 PDO 函数来保证您的代码的未来发展(有关更多信息,请参见:http: //php.net/manual/en/mysqlinfo.api.choosing。 php ).

您的 JavaScript 代码也存在一些问题。

$each(data.result, function(){

应该

$.each(data.result, function ( index, value ) {

您还应该更换

this['poza']

value.poza

最后你的 JavaScript 看起来像这样:

$(document).ready(function () {

    var updateList = function () {

        $.getJSON('cocktail.php', function( data ) {

            var list = $('ul');

            list.empty();

            $.each(data.results, function( index, result ) {

                list.append(
                    '<li>Poza: ' + result.poza + '</li>' +
                    '<li>Nume: ' + result.nume + '</li>' +
                    '<li>Compozitie: ' + result.compozitie + '</li>'
                );

            });

        });
    };

    window.setInterval(updateList, 200);

});
于 2013-10-15T17:40:02.987 回答