dimanche 5 avril 2015

MySQL SUBSTRING_INDEX Integrate with sqlite - PHP PDO

I am using PHP Data Objects to link the database. Below query is working fine with mysql.



$con = new PDO("mysql:host=localhost; dbname=company; charset=utf8", "root","root");

$data_value = $_GET['value'];
$sql = "SELECT SUBSTRING_INDEX(c_value, ',', 1) as c_index, SUBSTRING_INDEX(c_value, ',', -1) as c_name FROM `default_values` where category = 'Nationality'";

$smt = $con->prepare($sql);
$smt->execute();
$row = $smt->fetchAll(PDO::FETCH_OBJ);
print json_encode($row);


But when I connect to sqlite, I have to change the query to below code.



$con = new PDO('sqlite:d:/company.sqlite');
$data_value = $_GET['value'];
$sql = "SELECT substr(c_value,1, pos-1) as c_index, substr(c_value,pos+1) as c_name FROM (SELECT *, instr(c_value,',') as pos FROM default_values) WHERE category = 'Nationality';";

$smt = $con->prepare($sql);
$smt->execute();
$row = $smt->fetchAll(PDO::FETCH_OBJ);
print json_encode($row);


See Working MySQL Fiddle

See Working Sqlite Fiddle


Is there any way that we can do this with PHP-PDO, regardless of the database connection. I mean only change the db connection and without changing queries.


Thank You,

Supun


Aucun commentaire:

Enregistrer un commentaire