Page 1 of 1
I'm new to php so can't understand this
Posted: Fri Jul 09, 2010 2:24 pm
by Flycow
I'm new to php but have done some basic and pascal coding in the past as well as some elaborate spreadsheets so I can't understand why this
Code: Select all
$con = mysql_connect("localhost",$dbuser,$dbpassword);
mysql_select_db($dbname, $con);
$file=fopen($csvfile,"r");
$data = fgetcsv($file,1000,",");
$sql = 'CREATE TABLE $table (';
for ($i=1;$i<=count($data);$i++) {
$sql .= $data[$i] . 'VARCHAR(50), ';
}
mysql_query($sql,$con);
Produces a t_variable error on line 7
Re: I'm new to php so can't understand this
Posted: Fri Jul 09, 2010 3:00 pm
by requinix
a) Which one is line 7?
b) Variables don't work in single-quoted strings.
c) Your SQL is very incorrect.
Re: I'm new to php so can't understand this
Posted: Fri Jul 09, 2010 3:08 pm
by websitesca
Hey!
Firstly, you're gonna have trouble with:
You wanna use double quotes here. If you use single quotes, your $table variable will actually remain literally as '$table' and not get replaced with the value inside that variable.
I don't see any syntax errors here - I ran this code and it had no syntax errors.
I can see how you would get a runtime error however for your sql. You're gonna have a dangling comma at the end. You're gonna get something like:
Code: Select all
"Create table $table (name1 VARCHAR(50), name2 VARCHAR(50), "
So what you want to do is:
Code: Select all
$sql = substr($sql, 0, strlen($sql)) . ")";
Hope that helps!
Georges,
http://www.websites.ca
Re: I'm new to php so can't understand this
Posted: Fri Jul 09, 2010 3:20 pm
by Flycow
I ran it through again and the error disappeared and I'd figured the lack of an ending but thanks for your help