Ask the PDO import method

Source: Internet
Author: User
Ask the PDO import method to have a text. to import data to the database, use the PDO method.
First grade: Xiao Ming, Xiao Hong, Xiao ......, Tom
Grade 2: Small A, small B, small C ,...... Small Z
......
The data table to be imported is
Name, banji, other
James Grade 1 0
Xiaohong, grade 1, 0
..................
Sophomore year 0
.................. That is, the class for each name
Previously, file_get_contents was used in PHP to import and use preg_replace to replace the line feed with a separator.
Then, sort the array_filter and use the foreach ($ arr as $ value) method to import the data.
However, we can't write it out with PDO. please help me.


Reply to discussion (solution)

Paste your unsuccessful code

Paste your unsuccessful code


The following shows how to import only the individual text. if PDO is used, all PDO connections are correct, but neither of them can be imported successfully.
To use preprocessing, preprocessing is only effective for queries.
$ Dao = file_get_contents (text); $ dao = preg_replace ('/\ n | \ r \ n/', '|', $ dao ); $ arr = array_filter (explode ('|', $ dao); $ arr = array_chunk ($ arr, 1000); foreach ($ arr as $ value) {$ string = ''; foreach ($ value as $ v) {$ string. = "('$ V')," ;}$ string = trim ($ string,', '); mysql_query ("insert into biao VALUES ". $ string );}

In addition, by the way, you may have a question about the code written by one page in PDO:
A: write paging as an encapsulation class and call it wherever necessary.
B: Copy the paging code and add the modified code.
In Ignore repeated code writingIs A or B efficient ( Program processing)?

The data in each row is like this: Xiao Ming, Xiao Hong, Xiao ......, Tom
That is, "('$ V')," such as this "('First grade: James, Xiaohong, xiao ......, Xiaobai '),"
The table must have at least three columns, so the "insert into biao VALUES". $ string and other commands must fail.

The data in each row is like this: Xiao Ming, Xiao Hong, Xiao ......, Tom
That is, "('$ V')," such as this "('First grade: James, Xiaohong, xiao ......, Xiaobai '),"
The table must have at least three columns, so the "insert into biao VALUES". $ string and other commands must fail.


Hello, moderator. I noted it out. this is a separate field import, and I will not use PDO to import this field,
Now I want to know how to write with PDO and how to split and import multiple fields.

How to write mysql functions and PDO

$ S = <TXT first-grade: James, Xiaohong, Xiaobai second-grade: xiaoa, xiaob, xiaoc, xiaoztxt; $ ar = explode (PHP_EOL, $ s ); array_walk ($ ar, function (& $ v) {$ v = preg_split ('/: |,/', $ v) ;}); // print_r ($ ar ); // Build data end $ db = new PDO ('MySQL: dbname = test', 'root', ''); $ db-> query ('set names gbk '); $ db-> query ("create temporary table biao (name varchar (10), banji varchar (10), other int) charset = gbk "); $ smt = $ db-> prepare ("insert into biao values (?, ?, 0) "); // The database preparation ends with foreach ($ ar as $ v) {$ m = array_shift ($ v); foreach ($ v as $ t) $ smt-> execute (array ($ t, $ m);} // end of data insertion $ r = $ db-> query ("select * from biao ") -> fetchall (PDO: FETCH_ASSOC); print_r ($ r); // check the result.
Array ([0] => Array ([name] => Xiaoming [banji] => first-year [other] => 0) [1] => Array ([name] => Xiaohong [banji] => first-year [other] => 0) [2] => Array ([name] => small [banji] => first grade [other] => 0) [3] => Array ([name] => Xiao Bai [banji] => Grade 1 [other] => 0) [4] => Array ([name] => small A [banji] => second grade [other] => 0) [5] => Array ([name] => small B [banji] => second grade [other] => 0) [6] => Array ([name] => small C [banji] => second grade [other] => 0) [7] => Array ([name] => small Z [banji] => second grade [other] => 0 ))

Parse error: syntax error, unexpected T_FUNCTION in

[/Code]


Why did I report an error during my local test?

... In is followed by a line number. you can see which line it is.

... In is followed by a line number. you can see which line it is.


Array_walk ($ ar, function (& $ v ){
$ V = preg_split ('/: |,/', $ v );
});

The error array_walk ($ ar, function (& $ v ){
I looked at it, and there were no parentheses ......

That is, your php version is earlier than 5.3.
Don't keep yourself protected.

That is, your php version is earlier than 5.3.
Don't keep yourself protected.


PHP version (php_version): 5.5.25
PHP Version 5.5.25

If the code is correct, a complete error message is posted.

If the code is correct, a complete error message is posted.


Solved the problem. it turns out that there is a problem with the local version, and you can transfer it to the Internet.

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.