• Lucid Dreaming - Dream Views




    Results 1 to 8 of 8
    1. #1
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90

      MySQL | Dynamically Accessing a Table

      I have single database with multiple tables named, c1, c2, c3, and so on. I'm trying to access a certain table with the following query:
      PHP Code:
      $query="SELECT * FROM c'$id'"
      in which $id determines which table to access.

      This throws "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource".

      I've also tried it two other ways:
      PHP Code:
      $query="SELECT * FROM c.'$id'"
      PHP Code:
      $query="SELECT * FROM c+'$id'"
      Is it possible to do this, or is there another way to get around it?

      Thanks.
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    2. #2
      Shhh... I'm dreaming. exdreamer's Avatar
      Join Date
      May 2010
      Gender
      Location
      United Kingdom
      Posts
      92
      Likes
      15
      DJ Entries
      46
      PHP Code:
      //concatenate the strings
      $cid 'c'.$id;
      //use the string as the table name
      $query "SELECT * FROM $cid"
      If you were going to do it in a line, you could do:
      PHP Code:
      $query 'SELECT * FROM c'.$id'; 

    3. #3
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Pretty sure this would work too:
      PHP Code:
      $query "SELECT * FROM c$id"
      Generally, when you meet a problem like this, it can be a good idea just to print the string or whatever, so you can look at it and see why it's failing. Here it'd be because it read "SELECT * FROM c'1'" or somthing like that :p
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    4. #4
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      khh is corrent.

    5. #5
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      Thanks everyone! This thread can be locked
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    6. #6
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      Never mind about locking it! I have another problem.

      PHP Code:
      $query="INSERT INTO c$p ('id', 'Date', 'Name', 'Email', 'Entry') VALUES (NULL, NULL, '{$_POST[fname]}', '{$_POST[femail]}', '{$_POST[fentry]}')";

      if(!
      mysql_query($query))
      {
          die(
      'Error: ' mysql_error());

      The error report is:
      Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''id', 'Date', 'Name', 'Email', 'Entry') VALUES (NULL, NULL, 'sample name', 'samp' at line 1

      Help?
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    7. #7
      Achievements:
      Veteran First Class 5000 Hall Points
      reci's Avatar
      Join Date
      Feb 2008
      LD Count
      18
      Gender
      Location
      -
      Posts
      380
      Likes
      90
      again, nvm. I figured out the above probem.
      Tutorial: How to Fall Asleep Faster
      You are dreaming.Do a reality check.

    8. #8
      khh
      khh is offline
      Remember Achievements:
      1000 Hall Points Veteran First Class
      khh's Avatar
      Join Date
      Jun 2009
      Gender
      Location
      Norway
      Posts
      2,482
      Likes
      1309
      Out of curiosity, what was the problem?
      April Ryan is my friend,
      Every sorrow she can mend.
      When i visit her dark realm,
      Does it simply overwhelm.

    Similar Threads

    1. Accessing memories?
      By LucidFreedoM in forum Beyond Dreaming
      Replies: 4
      Last Post: 12-17-2008, 12:19 AM
    2. Make a table in db
      By Adam in forum Tech Talk
      Replies: 6
      Last Post: 05-20-2008, 11:37 AM
    3. Sun announces plans to buy MySQL
      By Ynot in forum Tech Talk
      Replies: 2
      Last Post: 01-19-2008, 03:56 AM
    4. Accessing Memories
      By Jrels in forum General Lucid Discussion
      Replies: 5
      Last Post: 06-15-2005, 10:41 PM
    5. Soo many books… What is on your table?
      By dream-scape in forum The Lounge
      Replies: 32
      Last Post: 11-02-2004, 09:40 PM

    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
    •