PHP converts Mysql data to a function_MySQL database in the SQLite format using the PDO method.

Source: Internet
Author: User
PHP uses the PDO method to convert Mysql data to a function in the SQLite form. Recently, a function is used to convert mysql data to sqlite data, because we want to save the image data in blob binary format, I will write a class myself. from the class, I will extract this method to share it. In order to continue working normally in the future, I used the PDO method for processing.

/*** Create SQlite * $ db database * $ tbname table name * $ is_images the binary array of images to be saved through Mysql ("img1", "img2", "img3 ") format. the default value is false * return SQL's array format */function creat_sqlite ($ db, $ tbname, $ is_images = false) {$ runsql1 = $ db-> query ("SELECT * FROM '$ tbname' limit 1"); $ fields_rows = $ runsql1-> columnCount (); $ tb_fields_info = array (); $ tb_string = ""; for ($ I = 0; $ I <$ fields_rows; $ I ++) {$ dot = ($ I> 0 )? ",": ""; $ Tb_meta = $ runsql1-> getColumnMeta ($ I ); // if ($ is_images & in_array ($ tb_meta ['name'], $ is_images) {continue;} $ tb_string. = $ dot. $ tb_meta ['name']; switch ($ tb_meta ['native _ type']) {case "VAR_STRING": $ tb_string. = "". "varchar"; break; case "LONG": $ tb_string. = "". "integer"; break; case "BLOB": $ tb_string. = "". "text"; break;} if (isset ($ tb_meta ['flags'] [1]) & ($ Tb_meta ['flags'] [1] === "primary_key") {$ tb_string. = "". "primary key";} if ($ tb_meta ['flags'] [0] === "not_null") {$ tb_string. = "". "not null" ;}}// SQL statement for data creation $ creat_tbs ['data'] = "CREATE TABLE IF NOT EXISTS data ($ tb_string );"; // if ($ is_images) {$ tb_images = ''; foreach ($ is_images as $ k => $ v) {$ di = ($ k> 0 )? ",": ""; $ Tb_images. = $ di. $ v. "blob not null";} $ creat_tbs ['images'] = "create table if not exists images (id integer primary key not null, tid integer not null, $ tb_images ); ";}return $ creat_tbs ;}

The result (image not set) generated by the demo is (I converted it to the json format ):


{"Data": "create table if not exists data (id integer primary key not null, name varchar not null, nickname varchar not null, emails varchar not null, passwd varchar not null, status not null );"}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.