1

I have converted sqlite database into mysql.

so then i need convert sqlite+php code into mysql+php code. do you have any document which containing methods to convert sqlite to mysql.

thanks..

this is my code

<?php

try {
  //open the database
  $db = new PDO('sqlite:cars.sqlite'); //will create the file in current directory. Current directory must be writable

  //create the database if does not exist
  $db->exec("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY, manufacturer TEXT, year INTEGER, price INTEGER)");

  //select all data from the table
  $select = $db->prepare('SELECT * FROM cars ORDER BY id ASC LIMIT 100');
  $select->execute();

  $out = array(
    'cars' => $select->fetchAll(PDO::FETCH_ASSOC)
  );
  echo json_encode($out);

  // close the database connection
  $db = NULL;
}
catch (PDOException $e) {
  print 'Exception : ' . $e->getMessage();
}
?>

mysql table:cars

CREATE TABLE IF NOT EXISTS `cars` (
  `id` int(11) NOT NULL DEFAULT '0',
  `manufacturer` text,
  `year` int(11) DEFAULT '0',
  `price` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

1 Answer 1

4

as you are using PDO, just create Mysql PDO object, change this part:

$db = new PDO('sqlite:cars.sqlite');

to something like:

$dsn = 'mysql:dbname=yourdbname;host=127.0.0.1';
$user = 'yourdbuser';
$password = 'yourdbpass';
$dbh = new PDO($dsn, $user, $password);

and you're done. You dont need to change anything other.

PDO is portable connection object, it can be used with several different database. more about PDO: http://php.net/manual/en/pdo.construct.php

Sign up to request clarification or add additional context in comments.

3 Comments

i added following db connection, $con=mysql_connect("localhost","root","") or die ("error1"); $db=mysql_select_db("decsys",$con) or die ("error2")
NO !, you are already using PDO. keep use it !. mysql_connect and friends is deprecated and will be removed soon. See my updated answer ?
Thanks a lot Mr.Charlie. you saved my day!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.