首页 > 解决方案 > PHP Mysql PDO 到 MSSQL /json /jquery /datatables

问题描述

我目前正在做一个项目,我开始研究并使用 Xampp 在 mysql 中制作它,但现在我想将其更改为 mssql,我得到了连接字符串,但数据表显示错误。

这是代码(适用于mysql):

    <?php
    include('db.php');
    include('function.php');
    $query = '';
    $output = array();
    $query .= "SELECT * FROM item ";

    $statement = $connection->prepare($query);
    $statement->execute();
    $result = $statement->fetchAll();
    $data = array();
    $filtered_rows = $statement->rowCount();
    foreach($result as $row)
    {

        $sub_array = array();
        $sub_array[] = $row["id"];
        $sub_array[] = $row["item_id"];
        $sub_array[] = $row["item_name"];
        $sub_array[] = $row["brand"];
        $sub_array[] = $row["description"];
        $sub_array[] = $row["category"];
        $sub_array[] = $row["serial_no"];
        $sub_array[] = $row["consumable"];
        $sub_array[] = $row["d_date"];
        $sub_array[] = $row["rs_no"];
        $sub_array[] = $row["rr_no"];
        $sub_array[] = $row["po_no"];
        $sub_array[] = $row["tag_no"];
        $sub_array[] = $row["transcode"];
        $sub_array[] = $row["supplier"];
        $sub_array[] = $row["d_added"];
        $sub_array[] = $row["p_account"];
        $sub_array[] = $row["c_account"];
        $sub_array[] = $row["status"];
        $sub_array[] = $row["con"];
        $sub_array[] = $row["location"];
        $data[] = $sub_array;
    }
    $output = array(
        "draw"              =>  intval($_POST["draw"]),
        "recordsTotal"      =>  $filtered_rows,
        "recordsFiltered"   =>  get_total_all_records(),
        "data"              =>  $data
    );
    echo json_encode($output);
    ?>

这是function.php的代码:

    <?php



    function get_total_all_records()
    {
        include('db.php');
        $statement = $connection->prepare("SELECT * FROM item");
        $statement->execute();
        $result = $statement->fetchAll();
        return $statement->rowCount();
    }

    ?>

对于 db.php:

    $connection = new PDO( "sqlsrv:Server=sqlserver;Database=testdb", "username","password");

当表加载它显示一个无效的 json 响应:

在此处输入图像描述

这是调试器捕获的错误:

致命错误:未捕获的 PDOException:SQLSTATE[08001]:[Microsoft][ODBC Driver 13 for SQL Server]TCP 提供程序:无法建立连接,因为目标计算机主动拒绝了它。在 C:\xampp\htdocs\material\upload\start1\examples\db.php:3 堆栈跟踪:#0 C:\xampp\htdocs\material\upload\start1\examples\db.php(3): PDO- >__construct('sqlsrv:Server=d...', '', '') #1 C:\xampp\htdocs\material\upload\start1\examples\fetch.php(2): include('C:\ xampp\htdocs...') #2 {main} 在第 3 行的 C:\xampp\htdocs\material\upload\start1\examples\db.php 中抛出。

我也在使用正确的凭据。

------更新2------

我已经设法在本地运行它,可能是远程服务器中的某些东西阻止了连接。

现在我正在尝试使用与 PDO mysql 一起使用的这行代码使其与数据表搜索一起使用。

    if(isset($_POST["search"]["value"]))
     {
      $query .= 'WHERE item_id LIKE "%'.$_POST["search"]["value"].'%"';
      $query .= 'OR item_name LIKE "%'.$_POST["search"]["value"].'%"';
      $query .= 'OR status LIKE "%'.$_POST["search"]["value"].'%"';
     }
    if(isset($_POST["order"]))
     {
      $query .= 'ORDER BY '.$_POST['order']['0']['column'].' '.$_POST['order'] ['0']['dir'].' ';
     }
   else
     {
   $query .= 'ORDER BY id DESC ';
     }
   if($_POST["length"] != 1)
     {
   $query .= 'LIMIT ' . $_POST['start'] . ', ' . $_POST['length'];
     }

------更新3------

你好!我设法让它运行,这只是由于 qoutes。

这是工作代码:

if(isset($_POST["search"]["value"]))
{
    $query .= "  WHERE item_id LIKE '%".$_POST["search"]["value"]."%' ";
    $query .= " OR item_name LIKE '%".$_POST["search"]["value"]."%' ";
    $query .= " OR status LIKE '%".$_POST["search"]["value"]."%' ";
}
if(isset($_POST["order"]))
{
    $query .= "ORDER BY '".$_POST['order']['0']['column']."' '".$_POST['order']['0']['dir']."' ";
}
else
{
    $query .= "ORDER BY id DESC ";
}  

我现在剩下的问题是 LIMIT 函数。即使在更改 qoutes 以适应 mssql 语法后,它也无法正常工作。提前。

if($_POST["length"] != -1)
{
    $query .= "LIMIT '" . $_POST['start'] . "', " . $_POST['length'];
}

标签: jqueryjsonsql-serverdatatable

解决方案


我认为您的 json 中缺少“user_data”行

但是为什么你把数组,数组里面,数组里面?

$data = array();
foreach($result as $k => $row)
{
   $data[$k] = $row;
}
$output = array(
    "draw"              =>  intval($_POST["draw"]),
    "recordsTotal"      =>  $filtered_rows,
    "recordsFiltered"   =>  get_total_all_records(),
    "data"              =>  $data
);
echo json_encode($output);

如果不起作用,请在网络下打开检查器并检查您的 json 响应


推荐阅读