/    Sign up×
Community /Pin to ProfileBookmark

Trouble creating multiple tables using a loops PHP/SQL DB

I want to know how to create multiple tables using a for loop in php code on a local sql db

i tried using this kind of code which is not working

for($i=0;$i=5;$i++)
{
$sql=”CREATE TABLE Hi_$i” (FirstName CHAR(30),LastName CHAR(30),Age INT)”;

// Execute query
if (mysqli_query($con,$sql))
{
echo “Table persons created successfully”;
}
else
{
echo “Error creating table: ” . mysqli_error($con);
}

any help
Thanks in advance

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJul 10.2013 — You are trying to create your table 5 times? Why? Once is sufficient and I'm sure that happened. You could add a 'die' clause to your query and output MySQL_error() when you do to see the error you are getting, but I think you want to re-think what you are doing here.
Copy linkTweet thisAlerts:
@ginerjmJul 10.2013 — Oops - didn't notice the variable $I was in the table name. My bad.

Anyway - the die clause would have helped you out. You should look at an example of a query execution to see how it s/b implemented in your querying.

That said - your query statement is bad. Actually if you turn on php error checking (another thing you should always do) it would have pointed you at the query line which has too many double quotes in it.
Copy linkTweet thisAlerts:
@c4gopalauthorJul 11.2013 — Oops - didn't notice the variable $I was in the table name. My bad.

Anyway - the die clause would have helped you out. You should look at an example of a query execution to see how it s/b implemented in your querying.

That said - your query statement is bad. Actually if you turn on php error checking (another thing you should always do) it would have pointed you at the query line which has too many double quotes in it.[/QUOTE]


Thanks a lot for your response...

I know that query statement is not right ...thats not what i am worried about ..

I was in a dilemma that whether i will be able to create 5 tables in a loop like that... tat was my first wondering and i simply added that Hi_$i without even knowing wether it works ..I am still a beginner in PHP so ..Bear with me .. after executing what happened is i got a infinite looping page where it was telling error creating the table ...see if u can help ? ? ? ? ? ?
Copy linkTweet thisAlerts:
@ginerjmJul 11.2013 — Well, with the query the way you are showing it, it CANNOT execute. So - if you have made a change AND you have turned on error checking, show us the new code and the exact error message(s) you are receiving.
×

Success!

Help @c4gopal 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...