Server-side script: It works with mysql and I can't make it work with sqlite...

Server-side script: It works with mysql and I can't make it work with sqlite...

mjesusmjesus Posts: 13Questions: 4Answers: 0

/* Database connection start */
$dsn = 'sqlite:/usr/share/nginx/html/sqliteent/dbent.db';
try {
$sqlite_db = new PDO($dsn);

} catch(PDOException $e) {
exit;
}

// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;

$columns = array(
// datatable column index => database column name
0 =>'id',
1 =>'nome',
2 =>'cmt',
3 =>'mrid'
);

// getting total number records without any search
$sql = "SELECT id, nome, cmt, mrid ";
$sql.=" FROM acao";

$query = $sqlite_db->query($sql) or die("acao_table_data_sqlite.php: get acao");
$totalData = count($query->fetchAll()); // para resgatar todo o array
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.

$sql = "SELECT id, nome, cmt, mrid FROM acao";
$sql.= "";
if( !empty($requestData['search']['value']) ) { // if there is a search parameter, $requestData['search']['value'] contains search parameter
$sql.=" AND ( id LIKE '" . $requestData['search']['value'] . "%' ";
$sql.=" OR nome LIKE '" . $requestData['search']['value'] . "%' ";
$sql.=" OR cmt LIKE '" . $requestData['search']['value'] . "%' ";
$sql.=" OR mrid LIKE '" . $requestData['search']['value'] . "%' ) ";
}

$query = $sqlite_db->query($sql) or die("acao_table_data_sqlite.php: get acao");
$totalFiltered = count($query->fetchAll()); // when there is no search parameter then total number rows = total number filtered rows.

$sql. = " ORDER BY " . $columns[$requestData['order'][0]['column']] . " " . $requestData['order'][0]['dir'] . " LIMIT " . $requestData['start'] . " ," . $requestData['length'] . " ";

/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query = $sqlite_db->query($sql) or die("Erro no sql: acao_table_data_sqlite.php: get acao");
$totalFiltered = count($query->fetchAll()); // when there is no search parameter then total number rows = total number filtered rows.

$data = array();

foreach ($sqlite_db->query($sql) as $row) { // preparing an array
$nestedData=array();

$nestedData[] = $row["id"];
$nestedData[] = $row["nome"];
$nestedData[] = $row["cmt"];
    $nestedData[] = $row["mrid"];

$data[] = $nestedData;

}

$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching,
"data" => $data // total data array
);

echo json_encode($json_data); // send data as json format

<?php > ?>

Answers

This discussion has been closed.