/    Sign up×
Community /Pin to ProfileBookmark

how can i create a mysql table in a for loop?

Just a slight problem in my coding. I’m trying to create a mysql table in a for loop but when i echo the sql statement it only pulls the last item in the array out.

for ($c=0; $c < $num; $c++) {
// echo $data[$c] . “<br />n”;
$sql = “CREATE TABLE ACR (
acrID int NOT NULL,
“.$data[$c].” varchar(100) NULL,
PRIMARY KEY (acrID))type=MyISAM”;

}
echo $sql;
}

How can i get it to pull all 187 $data variables inside the for loop instead of just the last $data variable?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 22.2008 — If you want to echo each SQL string, put the echo within the for loop, not after it.
Copy linkTweet thisAlerts:
@jblackwelderauthorDec 22.2008 — the echo is just there so i can see what the output of the sql statement is before i run it as an actual query. When i put the echo inside the for loop this is what the output is:

CREATE TABLE ACR ( acrID int NOT NULL, 1ASS varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAMCREATE TABLE ACR ( acrID int NOT NULL, 2M varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAMCREATE TABLE ACR ( acrID int NOT NULL, AB varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAMCREATE TABLE ACR ( acrID int NOT NULL, ADCO varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAMCREATE TABLE ACR ( acrID int NOT NULL, AGCY varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAMCREATE TABLE ACR ( acrID int NOT NULL, AGE0 varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAM.....etc.

but when the echo is outside the for loop i get just the last $data result:

CREATE TABLE ACR ( acrID int NOT NULL, zip varchar(100) NULL, PRIMARY KEY (acrID))type=MyISAM
Copy linkTweet thisAlerts:
@NogDogDec 22.2008 — I'm not real sure what the goal is here. If you want to end up with the variable $sql containing all of the queries created in the for loop, then you want to do something like:
[code=php]
$sql = "";
for(blah...blah...blah)
{
$sql .= "blah...blah...blahn"; // note use of ".=" opeartor
}
echo $sql;
[/code]

If the objective is something else, then I'll need more explanation.
Copy linkTweet thisAlerts:
@jblackwelderauthorDec 22.2008 — That did it!!! I have seen the .= around before i just never understood its meaning until now. The new code looks like this:

$sql = "CREATE TABLE ACR (acrID int NOT NULL,";

for ($c=0; $c < $num; $c++) {

$sql .= "".$data[$c]." varchar(100) NULL,";

}

$sql .= "PRIMARY KEY (acrID))type=MyISAM";

echo $sql;

}

And the output is exactly what i was looking for. Thanks alot for your knowledge and help?
×

Success!

Help @jblackwelder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...