Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Cannot combine MySQL queries into one?

  1. #1
    Join Date
    May 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cannot combine MySQL queries into one?

    When I try to tell MySQL to create two tables in a single query from php nothing shows up in the DB:

    Code:
    $query = 'CREATE TABLE table1( 
              cid INT NOT NULL AUTO_INCREMENT, 
              PRIMARY KEY(cid)); 
                 
              CREATE TABLE table2( 
              cid INT NOT NULL AUTO_INCREMENT, 
              PRIMARY KEY(cid))'; 
    mysql_query($query);
    If I split it into two queries it works fine:

    Code:
    $query = 'CREATE TABLE table1( 
              cid INT NOT NULL AUTO_INCREMENT, 
              PRIMARY KEY(cid))';
    mysql_query($query);
    
    $query = 'CREATE TABLE table2( 
              cid INT NOT NULL AUTO_INCREMENT, 
              PRIMARY KEY(cid))';
    mysql_query($query);
    Is there something wrong with my syntax? It works fine when I submit the query in phpmyadmin.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Did you try just removing the extra line break? (Between the two queries.)

    Since PHP is actually sending that exact code, it must be perfect... phpmyadmin may fix little things like extra line breaks, 'cause it's more user friendly (though PHP offers more control).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    May 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, I did. MySQL will yell at me if it's not present when I submit the query with phpmyadmin.

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Ah. Alright.
    Not really sure.

    I would think that PHP is able to do more than one at a time, though I'm not sure. Maybe the syntax is different... like ;, or something... but that doesn't really make sense.

    sorry.

    Someone should know soon.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    try outting a semicolon ( ; ) at the end of the secind query. see if that fixes it.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    May 2006
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    A MySQL query cannot end in a semi-colon.

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It can.

    The PHP mysql_query() is designed to return a recordset that is the result of that query. It doesn't make sense for it to be able to execute more than one query at once.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Ah, that makes sense.

    Is there a better function to execute multiple queries (not to retrieve anything, but to execute commands, like creating a table, etc.)?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't believe so, no.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,162
    Thanks
    263
    Thanked 690 Times in 678 Posts

    Default

    Would @mysql_query('ten commands here') work?
    It wouldn't return an error and would hopefully send the command through the sql processing engine...
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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