www.webdeveloper.com
Results 1 to 5 of 5

Thread: Trouble creating multiple tables using a loops PHP/SQL DB

  1. #1
    Join Date
    Jul 2013
    Posts
    2

    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

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    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.

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    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.

  4. #4
    Join Date
    Jul 2013
    Posts
    2

    Red face

    Quote Originally Posted by ginerjm View Post
    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.
    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

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    558
    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles