Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
处理ajax jquery数据库查询的最佳实践?_Jquery_Mysql_Ajax - Fatal编程技术网

处理ajax jquery数据库查询的最佳实践?

处理ajax jquery数据库查询的最佳实践?,jquery,mysql,ajax,Jquery,Mysql,Ajax,我有一个“小”问题,因为我不知道处理ajax jquery数据库请求的最佳方法是什么 到目前为止,我是这样做的: 对于永久(单个)数据库请求,我创建了一个新的.php文件,如下所示: newbill.php newcosumer.php listallbills.php updatecostumer.php …等,并通过以下方式提取数据: $.ajax({ type: "POST", url: "ajax/newbill.php", data: "..." succe

我有一个“小”问题,因为我不知道处理ajax jquery数据库请求的最佳方法是什么

到目前为止,我是这样做的: 对于永久(单个)数据库请求,我创建了一个新的.php文件,如下所示:

newbill.php
newcosumer.php
listallbills.php
updatecostumer.php
…等,并通过以下方式提取数据:

$.ajax({
 type: "POST",
    url: "ajax/newbill.php",
    data: "..."
    success: function(msg){    
    ...
    }
 }); 
…我不喜欢的是所有的.php文件都只针对一个!数据库请求

必须有更好的方法来处理这个问题???

您可以创建一个名为ie.
dbrequest.php的文件,并将参数传递给它,然后从php读取并向数据库发出相应的请求


但这取决于您在页面中的具体操作,如果在单个文件中很容易维护..

您可以创建一个名为ie.
dbrequest.php的文件,并将参数传递给它,然后从php读取并向数据库发出相应的请求


但这取决于您在页面中的具体操作,如果在单个文件中很容易维护,我会这样做

为每个实体(例如:consumerajax.php、billajax.php等)创建一个php文件,然后为每个数据库查询创建单独的函数。从javascript调用时,传递一个querystring变量以确定要在ajax服务器页面中执行的函数

例如:在consumerajax.php中

 <?php

    $mode= $_GET["mode"] ;
    if($mode=="getconsumerdet")
    {
       $consumerId=$_GET["cid"];
       GetConsumerDetails($consumerId)
    }
    else if($mode=="updateconsumer")
    {
      $consumerId=$_GET["cid"];
      $name=$_GET["name"];
      UpdateConsumerInfo($consumerId, $name)
    }

    function GetConsumerDetails($consumerId)
    {
       // execute query the table here and return the result from here
      echo"Have the data and return"
    } 
    function UpdateConsumerInfo($consumerId,$name)
    {
      //execute query to update
     echo "updated";
    }

?>


我会这样做的

为每个实体(例如:consumerajax.php、billajax.php等)创建一个php文件,然后为每个数据库查询创建单独的函数。从javascript调用时,传递一个querystring变量以确定要在ajax服务器页面中执行的函数

例如:在consumerajax.php中

 <?php

    $mode= $_GET["mode"] ;
    if($mode=="getconsumerdet")
    {
       $consumerId=$_GET["cid"];
       GetConsumerDetails($consumerId)
    }
    else if($mode=="updateconsumer")
    {
      $consumerId=$_GET["cid"];
      $name=$_GET["name"];
      UpdateConsumerInfo($consumerId, $name)
    }

    function GetConsumerDetails($consumerId)
    {
       // execute query the table here and return the result from here
      echo"Have the data and return"
    } 
    function UpdateConsumerInfo($consumerId,$name)
    {
      //execute query to update
     echo "updated";
    }

?>


您的具体问题是什么?这不是jquery问题。这实际上是关于服务器端模式和实现的。如果你在文章中添加PHP标签,你可能会得到更好的答案。你的特殊问题是什么?这不是一个jquery问题。这实际上是关于服务器端模式和实现的。如果你在帖子中添加PHP标记,你可能会得到更好的答案。thx,就是这样,我也在考虑类似的事情(使用这种helper变量=$mode),但我认为可能有更好的方法直接处理函数(或类中的方法)?thx,就是这样,我也在考虑类似的事情(使用这种类型的helper变量=$mode)但我认为可能有更好的方法直接处理函数(或类中的方法)?我的想法是为DB查询提供一个Datamanager类,但可能一个包含函数的普通脚本文件就可以了(如Shyju所示)?如果可以简单,为什么它会变得复杂:)我的想法是为DB查询提供一个Datamanager类,但也许一个带有函数的普通脚本文件就可以了(如Shyju所示)?当它可以很简单时,为什么它会很复杂:)
  $.ajax({ type: "POST", url: "ajax/consumerajax.php?mode=updateconsumer&cid=34&name=shyju", data: "..." success: function(msg){   });