1

我不知道出了什么问题,问题是我试图从 mysql 查询中返回值以在 html div (#receptor)中打印,但没有打印任何值,我试图在 make 之后进行调用对下面定义的外部函数的请求以打印 JSON 源,这就是我现在要做的:

结果.php

    <?php
    $library = "DbConnection.inc.php";
    if (is_readable($library)){
            require $library;}else{
            throw new RuntimeException("No se Pudo Incluir la ${library}");
    }


    $db = new DbConnection('localhost','root','','macrotelecom');

    switch (@$_REQUEST['action'])
    {
       case "consultar" :
        $db->connect();
        $data =  $db->getAllRows("SELECT * FROM Caracter");
        $db->disconnect();    
        echo json_encode($data);      
        exit;


    }


    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src='jquery-1.7.1.min.js' ></script>
    <script type="text/javascript">
    $(function(){ 
      $("#consultar").click(function(){
        $("#receptor").text("Consultando....");        
          $.post("result.php",{action:"consultar"}, respuesta,'json')
       });
    });



    function respuesta(arg){

       $("#receptor").html(arg.toSource());
    }
    </script>
    </head>
    <body>
        </br>
        <input type="button" id="consultar" value="consultar">
        <div id="receptor" style="border: solid 1px black; height: 100%; width: 100%;">

        </div>
    </body>
    </html>

但是,如果我在 $.post 函数中进行调用,它可以工作,但我不想这样做:

    <?php
    $library = "DbConnection.inc.php";
    if (is_readable($library)){
            require $library;}else{
            throw new RuntimeException("No se Pudo Incluir la ${library}");
    }


    $db = new DbConnection('localhost','root','','macrotelecom');

    switch (@$_REQUEST['action'])
    {
       case "consultar" :
        $db->connect();
        $data =  $db->getAllRows("SELECT * FROM Caracter");
        $db->disconnect();    
        echo json_encode($data);      
        exit;


    }


    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src='jquery-1.7.1.min.js' ></script>
    <script type="text/javascript">
    $(function(){ 
      $("#consultar").click(function(){
        $("#receptor").text("Consultando....");        
          $.post("result.php",{action:"consultar"}, respuesta,'json')
       });
    });



    function respuesta(arg){

       $("#receptor").html(arg.toSource());
    }
    </script>
    </head>
    <body>
        </br>
        <input type="button" id="consultar" value="consultar">
        <div id="receptor" style="border: solid 1px black; height: 100%; width: 100%;">

        </div>
    </body>
    </html>

怎么了,这让我发疯了,我无法理解为什么不打印值,我将 de 数据类型定义为 json 并且没有任何反应,我在调用“respuesta”中传递了参数,如 respuesta(data) 并且两者都没有,我希望有人可以帮帮我,我看不到解决方案。

如果您想进行本地测试,我将库代码用于我正在使用的数据库连接。

数据库连接.inc.php

    <?php
    class DbConnection {

      private $db_connection  = null;
      private $db_host     = '';
      private $db_user     = '';
      private $db_password = '';
      private $db_name     = '';
      private $errors      = array();



      public function __construct($db_host, $db_user, $db_password, $db_name)
      {
        $this->db_host     = $db_host;
        $this->db_user     = $db_user;
        $this->db_password = $db_password;
        $this->db_name     = $db_name;
      }

      public function connect()
      {
        if ( !$this->db_connection = @mysql_connect($this->db_host, $this->db_user, $this->db_password) ) {
          throw new RunTimeException("Couldn't connect to the database server");
        }
        if ( !@mysql_select_db($this->db_name, $this->db_connection) ) {
          throw new RunTimeException("Couldn't connect to the given database");
        }
        $this->executeQuery("SET CHARACTER SET 'utf8'");
      }

      public function disconnect(){
        if(mysql_close($this->db_connection)){
            return true;
        }
        return false;
      }

      public function getAllRows($sql)
      {
        if ( !$results = @mysql_query($sql, $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        $count = 0;
        $rows  = array();
        while ( $row = mysql_fetch_assoc($results) ) {
          $rows[] = $row;
          $count++;
        }
        return ($count)?$rows:false;
      }

      public function getOneColumn($sql)
      {
        if ( !$results = @mysql_query($sql, $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        $count = 0;
        $rows  = array();
        while ( $row = mysql_fetch_array($results) ) {
          $rows[] = $row[0];
          $count++;
        }
        return ($count)?$rows:false;
      }

      public function getArrayPair($sql)
      {
        if ( !$results = @mysql_query($sql, $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        $count = 0;
        $rows  = array();
        while ( $row = mysql_fetch_array($results) ) {
          $rows[$row[0]] = $row[1];
          $count++;
        }
        return ($count)?$rows:false;
      }

      public function getOneRow($sql)
      {
        if ( !$results = @mysql_query($sql, $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        if ( $row = mysql_fetch_assoc($results) ) {
          return $row;
        }
        return false;
      }

      public function getOneValue($sql)
      {
        if ( !$results = @mysql_query($sql, $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        if ( $row = mysql_fetch_array($results) ) {
          return $row[0];
        }
        return false;
      }

      public function executeQuery($sql)
      {
        if ( !@mysql_query($sql, $this->db_connection) ) {
          $this->errors[] = mysql_error($this->db_connection);
          return false;
        }
        return true;
      }

      public function getErrors()
      {
        return $this->errors;
      }

      public function getLastId()
      {
        return mysql_insert_id($this->db_connection);
      }

      public function countRows($table)
      {
        if (!is_string($table)) {
          throw new InvalidArgumentException("table_name isn't an string");
        }

        if ( !$results = @mysql_query("SELECT COUNT(*) as total FROM $table", $this->db_connection) ) {
          throw new RunTimeException("Couldn't execute query: ". mysql_error($this->db_connection) );
        }

        $count = mysql_fetch_array($results);
        $count = $count['total'];
        return ($count)?$count:0;
      }
    }

    ?>
4

1 回答 1

1

你也可以用这个

var id=$("#id").attr("value");
$.getJSON("pages.php",{id:id},dates);
function dates(datos)
{

$("#list").html("Name:"+datos[1].name+"<br>"+"Last Name:"+datos[1].lastname+"           
 <br>"+"Address:"+datos[1].address);
}

更多帮助你会在这里找到

http://docs.jquery.com/Getjson

希望这会有所帮助

于 2012-08-23T14:32:10.437 回答