简体   繁体   中英

How to fix error Query array failed: 1064

I want to export the data in the datatable to an excel file and then an error like this appears

Query array failed: 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 4

this is using ajax:

            $generic = \core\app\classes\generic\generic::getInstance();
            $core_db = new \core\app\classes\core_db\core_db;
            $countries = $core_db->getAllCountryCodes();

            $spreadsheet = new Spreadsheet();
            $sheet = $spreadsheet->getActiveSheet();
            $style_border = array(

                'borders' => array(
                    'allBorders' => array(
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                        'color' => array('rgb' => '000000'),
                    ),
                ),
            );
            $style_header = array(
                'font' => [
                    'bold' => true
                ],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                    'startColor' => [
                        'rgb' => 'f2f2f2',
                    ]
                ]
            );

            $row = 1;

            $sheet->setCellValue('A' . $row, 'No.');
            $sheet->setCellValue('B' . $row, 'Name');
            $sheet->setCellValue('C' . $row, 'Email');
            $sheet->setCellValue('D' . $row, 'Country');
            $sheet->setCellValue('E' . $row, 'LP');
            $sheet->setCellValue('F' . $row, 'LEP');
            $sheet->setCellValue('G' . $row, 'Created On');
            $sheet->setCellValue('H' . $row, 'Link');
            $sheet->getStyle('A' . $row . ':H' . $row)->applyFromArray($style_header);

            $sheet->getColumnDimension('A')->setAutoSize(true);
            $sheet->getColumnDimension('B')->setAutoSize(true);
            $sheet->getColumnDimension('C')->setAutoSize(true);
            $sheet->getColumnDimension('D')->setAutoSize(true);
            $sheet->getColumnDimension('E')->setAutoSize(true);
            $sheet->getColumnDimension('F')->setAutoSize(true);
            $sheet->getColumnDimension('G')->setAutoSize(true);
            $sheet->getColumnDimension('H')->setAutoSize(true);

            $data_cv = $cv_db->exportAllVerifiedCV($_POST);
            $i = ($row - 1);
            $link = HTTP_TYPE . SITE_WWW;
            foreach ($data_cv as $key => $value) {
                $fullname = $generic->getName('per', $value['entity_family_name'], $value['number_given_name'], ADDRESS_BOOK_OUTPUT_PER_NAME, ADDRESS_BOOK_OUTPUT_ENT_NAME);

                $full_country = isset($countries[$value['country']]) ? $countries[$value['country']] : $value['country'];

                $i++;
                $sheet->setCellValue('A' . ($row + $i), $i);
                $sheet->setCellValue('B' . ($row + $i), $fullname);
                $sheet->setCellValue('C' . ($row + $i), $value['main_email']);
                $sheet->setCellValue('D' . ($row + $i), $full_country);
                $sheet->setCellValue('E' . ($row + $i), $value['partner_name']);
                $sheet->setCellValue('F' . ($row + $i), $value['partner_lep_name']);
                $sheet->setCellValue('G' . ($row + $i), date('d M Y', strtotime($value['created_on'])));
                $sheet->setCellValue('H' . ($row + $i), $link . "/cv/share/" . $value['hash']);
                $sheet->getCell('H' . ($row + $i))
                    ->getHyperlink()
                    ->setUrl($link . "/cv/share/" . $value['hash']);
            }
            $sheet->getStyle('A' . $row . ':H' . ($row + $i))->applyFromArray($style_border);

            $writer = new Xlsx($spreadsheet);
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="List_cv_' . date('Y-m-d') . '.xlsx"');
            header('Cache-Control: max-age=0');
            ob_end_clean();
            $writer->save('php://output');

then this is a query to generate the data from the database:

    public function exportAllVerifiedCV($request)
{
    $request = $_POST;
    $table = 'personal';

    $primaryKey = 'personal.address_book_id';

    $columns = array(
        array('db' => 'personal.address_book_id', 'dt' => 'address_book_id'),
        array('db' => 'address_book_address.country', 'dt' => 'country'),
        array('db' => 'address_book_per.title', 'dt' => 'title'),
        array('db' => 'address_book_per.middle_names', 'dt' => 'middle_names'),
        array('db' => 'address_book.number_given_name', 'dt' => 'number_given_name'),
        array('db' => 'address_book.entity_family_name', 'dt' => 'entity_family_name'),
        array('db' => 'address_book.main_email', 'dt' => 'main_email'),
        array('db' => 'personal.created_on', 'dt' => 'created_on'),
        array('db' => 'address_book.created_by', 'dt' => 'created_by'),

        array('db' => 'partner.entity_family_name', 'as' => 'partner_name',  'dt' => 'partner_name'),
        array('db' => 'partner.address_book_id', 'as' => 'partner_id',  'dt' => 'partner_id'),
        array('db' => 'address_book.address_book_id', 'dt' => 'address_book_id'),
        array('db' => 'personal_cv.hash', 'dt' => 'hash')
    );


    $limit = $this->limit($request, $columns);
    $order = $this->order($request, $columns);

    $join = ' JOIN `address_book` ON `personal`.`address_book_id` = `address_book`.`address_book_id` ';
    $join .= ' JOIN `address_book_per` ON `personal`.`address_book_id` = `address_book_per`.`address_book_id` ';
    $join .= ' LEFT JOIN `address_book_address` ON `address_book`.`address_book_id` = `address_book_address`.`address_book_id` ';

    $join .= ' LEFT JOIN address_book_connection on address_book_connection.address_book_id = personal.address_book_id AND `address_book_connection`.`connection_type`="lp"';
    $join .= ' LEFT JOIN `address_book_connection` as `partner_lep` ON `personal`.`address_book_id` = `partner_lep`.`address_book_id` AND `partner_lep`.`connection_type`="lep"';
    $join .= ' LEFT JOIN `address_book` as `partner` ON `address_book_connection`.`connection_id` = `partner`.`address_book_id` ';
    $join .= ' LEFT JOIN `personal_cv` ON `personal_cv`.`address_book_id` = `personal`.`address_book_id` ';

    $where = $this->filter($request, $columns, $bindings);

    $where .= (strpos(strtolower($where), 'where') === false) ? ' WHERE ' :  ' AND ';
    $where .= " `personal`.`status` = 'verified' ";
    $where .= (strpos(strtolower($where), 'where') === false) ? ' WHERE ' :  ' AND ';
    $where .= " `personal`.`address_book_id` in (".$request['address_book_id'].")";

    $qry1 = "SELECT " . implode(", ", self::pluck($columns, 'db')) . "
         FROM `$table`
         $join
         $where
         $order
         $limit";
    $data = $this->db->query_array($qry1);
    return $data;

}

Most likely " personal.address_book_id in (".$request['address_book_id'].")" causing issue.

  • If it's array, then you should implode(', ', $request[])
  • If not array, then no need for IN .

Also it's open to SQL injection and if address_book_id is not numeric, then each element must be enclosed in quotes


What class is $this->db ? Maybe it has proper query builder?

The problem with the error has been resolved, thanks to those of you who have given me advice:) @justinas and @brombeer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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