简体   繁体   English

Laravel sql 查询在从 ajax 调用时出错

[英]Laravel sql query is giving error while calling from ajax

I have reg_dental_camp table having columns id, reg_name, reg_email, reg_phone, reg_type, reg_info, created_at, updated_at我有reg_dental_campid, reg_name, reg_email, reg_phone, reg_type, reg_info, created_at, updated_at

I want to fetch the table data in Laravel and show them using ajax.我想获取 Laravel 中的表数据并使用 ajax 显示它们。

The function I have written is:我写的function是:

public function showUsers(){
     $table ="SELECT * FROM `reg_dental_camp`";
     $primaryKey = 'id';
     $userColumns = array(
          array( 'db' => 'id',  'dt' => 'id' ),
          array( 'db' => 'reg_name', 'dt' => 'reg_name' ),
          array( 'db' => 'reg_email', 'dt' => 'reg_email' ),
          array( 'db' => 'reg_phone', 'dt' => 'reg_phone' ),
          array( 'db' => 'reg_type', 'dt' => 'reg_type' ),
          array( 'db' => 'reg_info', 'dt' => 'reg_info' ),
          array(
              'db'        => 'created_at',
              'dt'        => 'created_at',
              'formatter' => function( $d, $row ) {
                  return date( 'jS M y', strtotime($d));
              }
          )
      );
      if($_SERVER['HTTP_HOST']=='localhost'){
          $sql_details = array(
              'user' => 'root',
              'pass' => '',
              'db'   => 'ubl',
              'host' => 'localhost'
          );
      }
      

      $userResult =  SSP::simple( $_GET, $sql_details, $table, $primaryKey, $userColumns);

      print_r($userResult);
}

While visiting from the main page it is showing error number 500 on ajax call URL and visiting directly to the ajax call URL is is giving SQL error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM `reg_dental_camp` While visiting from the main page it is showing error number 500 on ajax call URL and visiting directly to the ajax call URL is is giving SQL error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM `reg_dental_camp` SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM `reg_dental_camp`

How can I solve this problem?我怎么解决这个问题? Thanks in advance提前致谢

Your are using simple method wrongly您错误地使用了简单的方法

Simple implementation from GitHub GitHub的简单实现

static function simple ( $request, $conn, $table, $primaryKey, $columns )
{
    $bindings = array();
    $db = self::db( $conn );

    // Build the SQL query string from the request
    $limit = self::limit( $request, $columns );
    $order = self::order( $request, $columns );
    $where = self::filter( $request, $columns, $bindings );

    // Main query to actually get the data
    $data = self::sql_exec( $db, $bindings,
        "SELECT `".implode("`, `", self::pluck($columns, 'db'))."`
         FROM `$table`
         $where
         $order
         $limit"
    );

    // Data set length after filtering
    $resFilterLength = self::sql_exec( $db, $bindings,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table`
         $where"
    );
    $recordsFiltered = $resFilterLength[0][0];

    // Total data set length
    $resTotalLength = self::sql_exec( $db,
        "SELECT COUNT(`{$primaryKey}`)
         FROM   `$table`"
    );
    $recordsTotal = $resTotalLength[0][0];

    /*
     * Output
     */
    return array(
        "draw"            => isset ( $request['draw'] ) ?
            intval( $request['draw'] ) :
            0,
        "recordsTotal"    => intval( $recordsTotal ),
        "recordsFiltered" => intval( $recordsFiltered ),
        "data"            => self::data_output( $columns, $data )
    );
}

So you need to provide table name only not a query.所以你只需要提供表名而不是查询。 Change table variable to将表变量更改为

$table = "reg_dental_camp"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM