Solved

use php to create mysql insert code

Posted on 2013-11-07
5
474 Views
Last Modified: 2013-11-11
select column1, column2, column3, column4 from table1

I copy paste results in mysql workbench

DTw  DTw  3658976250  staff3
DTw  DTw  3658422687  staff3
DTw  DTw  3653399588  staff3
DTw  DTw  3653174764  staff3
DTw  DTw  3546098525  staff3
DTw  DTw  3545571594  staff3
DTw  DTw  3545571593  staff3
DTw  DTw  3540574071  staff3
DTw  DTw  3540454344  staff3
DTw  DTw  3540376883  staff3
fre  fre  3544889093  staff3
gam  gam  3955910171  staff3
gam  gam  3955910165  staff3
gam  gam  3649458031  staff3
gam  gam  3648816297  staff3
gam  gam  3648783076  staff3
gam  gam  3648516109  staff3
gam  gam  3647758123  staff3
gam  gam  3554774961  staff3
gam  gam  3540425395  staff3
gam  gam  3540357159  staff3
gam  gam  3540345288  staff3
h1a  staff3  3700557963  staff3
h1a  h1a  3693946789  staff3
h1a  h1a  3693806133  staff3
h1a  staff3  3684800156  staff3
h1a  h1a  3684173904  staff3
h1a  h1a  3682095987  staff3
h1a  h1a  3682095983  staff3
h1a  h1a  3542388954  staff3
nyh  nyh  3554817690  staff3
nyh  nyh  3554796334  staff3
nyh  nyh  3554701760  staff3
nyh  nyh  3554689575  staff3
nyh  nyh  3541014726  staff3
nyh  nyh  3541012663  staff3
nyh  nyh  3540619436  staff3
nyh  nyh  3540474531  staff3

Open in new window



want output
('DTw', 'DTw', '3658976250', 'staff3'),
('DTw', 'DTw', '3658422687', 'staff3'),
('DTw', 'DTw', '3653399588', 'staff3'),
('DTw', 'DTw', '3653174764', 'staff3'),
('DTw', 'DTw', '3546098525', 'staff3'),
('DTw', 'DTw', '3545571594', 'staff3'),
('DTw', 'DTw', '3545571593', 'staff3'),
('DTw', 'DTw', '3540574071', 'staff3'),
('DTw', 'DTw', '3540454344', 'staff3'),
('DTw', 'DTw', '3540376883', 'staff3'),
('fre', 'fre', '3544889093', 'staff3'),
('gam', 'gam', '3955910171', 'staff3'),
('gam', 'gam', '3955910165', 'staff3'),
('gam', 'gam', '3649458031', 'staff3'),
('gam', 'gam', '3648816297', 'staff3'),
('gam', 'gam', '3648783076', 'staff3'),
('gam', 'gam', '3648516109', 'staff3'),
('gam', 'gam', '3647758123', 'staff3'),
('gam', 'gam', '3554774961', 'staff3'),
('gam', 'gam', '3540425395', 'staff3'),
('gam', 'gam', '3540357159', 'staff3'),
('gam', 'gam', '3540345288', 'staff3'),
('h1a', 'staff3', '3700557963', 'staff3'),
('h1a', 'h1a', '3693946789', 'staff3'),
('h1a', 'h1a', '3693806133', 'staff3'),
('h1a', 'staff3', '3684800156', 'staff3'),
('h1a', 'h1a', '3684173904', 'staff3'),
('h1a', 'h1a', '3682095987', 'staff3'),
('h1a', 'h1a', '3682095983', 'staff3'),
('h1a', 'h1a', '3542388954', 'staff3'),
('nyh', 'nyh', '3554817690', 'staff3'),
('nyh', 'nyh', '3554796334', 'staff3'),
('nyh', 'nyh', '3554701760', 'staff3'),
('nyh', 'nyh', '3554689575', 'staff3'),
('nyh', 'nyh', '3541014726', 'staff3'),
('nyh', 'nyh', '3541012663', 'staff3'),
('nyh', 'nyh', '3540619436', 'staff3'),
('nyh', 'nyh', '3540474531', 'staff3'),

Open in new window



the reason why I want this is because I want to allow expert to insert the same rows I have in my mysql table
0
Comment
Question by:rgb192
  • 2
  • 2
5 Comments
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39629765
Not sure I follow. Are you wanting to select the rows from 1 table and insert into another. Are you trying to do this in mysql workbench or do you need to do it in PHP?
0
 

Author Comment

by:rgb192
ID: 39630840
two code blocks

I have included the input which I would copy paste or read in from input.txt

and using php, I would like the output echoed to screen or written to output file


In this example I had to copy paste
','
over 100 times
0
 
LVL 42

Expert Comment

by:Chris Stanyon
ID: 39630994
Have a look at this:

<?php 
$file = fopen('data.txt', 'r');

while ($line = fgets($file)):
	$data = explode("  ", trim($line));
	array_walk($data, create_function('&$str', '$str = "\'$str\'";'));
	$newData[] = sprintf("(%s)", implode(',', $data));
endwhile;

echo implode(',', $newData);

Open in new window

It will read your data from a file called data.txt, and split each line on a double space. It will wrap each element in single quotes and then the whole line in brackets. Finally it adds the comma to the end of each line.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39633311
As usual, the quality of the solution will be highly dependent on the quality of the test data.

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28287733.html

// SIMULATE READING THE EXTERNAL FILE WITH file_get_contents();
$doc = <<<EOD
DTw  DTw  3658976250  staff3
DTw  DTw  3658422687  staff3
DTw  DTw  3653399588  staff3
DTw  DTw  3653174764  staff3
DTw  DTw  3546098525  staff3
DTw  DTw  3545571594  staff3
DTw  DTw  3545571593  staff3
DTw  DTw  3540574071  staff3
DTw  DTw  3540454344  staff3
DTw  DTw  3540376883  staff3
fre  fre  3544889093  staff3
gam  gam  3955910171  staff3
gam  gam  3955910165  staff3
gam  gam  3649458031  staff3
gam  gam  3648816297  staff3
gam  gam  3648783076  staff3
gam  gam  3648516109  staff3
gam  gam  3647758123  staff3
gam  gam  3554774961  staff3
gam  gam  3540425395  staff3
gam  gam  3540357159  staff3
gam  gam  3540345288  staff3
h1a  staff3  3700557963  staff3
h1a  h1a  3693946789  staff3
h1a  h1a  3693806133  staff3
h1a  staff3  3684800156  staff3
h1a  h1a  3684173904  staff3
h1a  h1a  3682095987  staff3
h1a  h1a  3682095983  staff3
h1a  h1a  3542388954  staff3
nyh  nyh  3554817690  staff3
nyh  nyh  3554796334  staff3
nyh  nyh  3554701760  staff3
nyh  nyh  3554689575  staff3
nyh  nyh  3541014726  staff3
nyh  nyh  3541012663  staff3
nyh  nyh  3540619436  staff3
nyh  nyh  3540474531  staff3
EOD;

// BREAK THE DOCUMENT INTO INDIVIDUAL LINES
$arr = explode(PHP_EOL , $doc);

// PROCESS EACH LINE
$out = array();
foreach ($arr as $str)
{
    // ELIMINATE UNWANTED WHITESPACE
    $str = preg_replace('/\s\s+/', ' ', $str);
    $str = trim($str);

    // RECONSTRUCT INTO THE QUERY FORMAT
    $dat = explode(' ', $str);
    $out[] = "('" . implode("', '", $dat) . "')";
}

// BUILD THE QUERY STRING
$new = implode(',' . PHP_EOL, $out);
echo $new;

Open in new window

HTH, ~Ray
0
 

Author Closing Comment

by:rgb192
ID: 39639676
thanks this works
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now