473,549 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create a mySQL table from the field headings line of a CSV file

1 New Member
What I'm looking for is a lazy man's way to define a table structure, using the table headings row in a CSV file. I'd be happy to start with all fields being VARCHAR(50) or some such default, and then making appropriate changes via PHPmyAdmin later.

The reason for being this lazy is the number of fields that I need to define - the CSV file is downloaded from a hotel booking affiliate, and contains every conceivable detail about the hotels. If they were also using mySQL, I could have asked if they'd provide a dump instead of a CSV file...

Anyone have any bright ideas?
Dec 9 '07 #1
3 32183
ronverdonk
4,258 Recognized Expert Specialist
As follows (maybe a bit late):
[php]<?php
$handle = fopen("test.csv ", "r");
// Read first (headers) record only)
$data = fgetcsv($handle , 1000, ",");
$sql= 'CREATE TABLE table_name (';
for($i=0;$i<cou nt($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
$sql .= ')';
echo $sql;
fclose($handle) ;
?> [/php]Ronald
Feb 18 '08 #2
Gerald Ekosso
1 New Member
There is a small problem when you try to run the resulting query in MYSQL. That is because there is an extra comma "," resulting from the generated SQL. I got around it by adding one line more. Code below:

<?php
$handle = fopen("Master_G erald.csv", "r");
// Read first (headers) record only)
$data = fgetcsv($handle , 1000, ",");
$sql= 'CREATE TABLE table_name (';
for($i=0;$i<cou nt($data); $i++) {
$sql .= $data[$i].' VARCHAR(50), ';
}
//The line below gets rid of the comma
$sql = substr($sql,0,s trlen($sql)-2);
$sql .= ')';
echo $sql;
fclose($handle) ;
?>
Sep 14 '10 #3
dkdenni7
1 New Member
hi in this by default varchar , how i change data according to data ....?????
Mar 1 '17 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5255
by: Fredo | last post by:
Hello, I have this .txt file : Roger|tow25$rank259 Isabelle|tow36$rank24 Pascal|tow12$rank29 Sergeï|tow45$rank5 Michel|tow1245$rank45478 Frédéric|tow1$rank125425
3
1600
by: changli_to | last post by:
Is there a build-in file or package?
1
1663
by: E Arredondo | last post by:
Is there a way to create a MYSQL table or database using a text file that countains the Field name and the lenght ? that is : fields.txt ----- start ---------- name,10 address,15
3
1745
by: prabhukumarasamy | last post by:
Now i m working in a existing project. I have to do some updations. In that project database table contain a table(usergroups) field name as 'Name'. When i am trying to insert a new record in (usergoups) table like as follows' insert into UserGroups(Name,modusr,moddt) values ('sam',131,'7/23/2006 10:02:13 PM')
4
3427
by: mramsay | last post by:
Hi, I'm having a real problem creating a dynamic hyperlink for my website. I want to pull the field name from mysql table. Field name is description. I would like this to be a hyperlink on my homepage. Also, when the user clicks on the description hyperlink they should see a dropdown with other hyperlinks such as hockey, baseball, etc. ...
2
6592
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say select * from tablename) but when i am trying redirect output to file in Linux it shows error? can you tell me suggestion for this issue. code i...
1
1336
by: Kollypap | last post by:
Im working on a project that involves PATIENT ENROLMENT, having patient's ID as key. there is possibility of a patient to have several follow up visits after been enrolled (meanwhile for each follow-up visit i will have to keep a complete record for each patient at every visit like: visit-date, purpose, recommendations ...). So i want to have...
1
2850
by: lipton | last post by:
how can i compare a mysql table feild value with selected redio button value with php
0
7520
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7450
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7720
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7809
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3500
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1941
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1059
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.