Hack Forums
[PHP] Is There Any Way to Randomly Generate A Database's or a Table's Name - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] Is There Any Way to Randomly Generate A Database's or a Table's Name (/showthread.php?tid=5995391)



Is There Any Way to Randomly Generate A Database's or a Table's Name - Effexion - Aug 2, 2019

Let's say I need to store a dataset of around 16 columns and each user needs their own database or table. I obviously can't hardcode the database's name:

https://pastebin.com/9Xa5jB0y

So, I was thinking of making "DATABASE_NAME" a variable but I just can't fathom how I would do this (after a little research it seems this would be the easiest solution).

Anyone have any solutions?


RE: Is There Any Way to Randomly Generate A Database's or a Table's Name - tweety - Aug 2, 2019

(Aug 2, 2019 05:40 AM)Effexion Wrote:  Let's say I need to store a dataset of around 16 columns and each user needs their own database or table. I obviously can't hardcode the database's name:

https://pastebin.com/9Xa5jB0y

So, I was thinking of making "DATABASE_NAME" a variable but I just can't fathom how I would do this (after a little research it seems this would be the easiest solution).

Anyone have any solutions?

just use a random string?


RE: Is There Any Way to Randomly Generate A Database's or a Table's Name - Effexion - Aug 2, 2019

(Aug 2, 2019 05:48 AM)tweety Wrote:  just use a random string?

That wouldn't work as it would still be hardcoded. I need it to be dynamic.


RE: Is There Any Way to Randomly Generate A Database's or a Table's Name - sdk - Aug 2, 2019

(Aug 2, 2019 05:49 AM)Effexion Wrote:  That wouldn't work as it would still be hardcoded. I need it to be dynamic.

He means generate the random string, But personally you could use the usersId to make sure you can make the relation.. If you have todo this for just 16 columns see my later explanation, relationships are you're best friend in databases

Code
db.main {
  table.users {
    id
    email
    ...etc that doesnt need to be in the other DB
  }
}

db.userId {
  ...stuff
}

All though I would personally say this is bad design, most things like this can simply be taken care of with relations

Code
db.main {
    table.users {
        id
        email
        ...etc
    }
    table.usersStuff {
        id
        userId
        ...etc
    }
}



RE: Is There Any Way to Randomly Generate A Database's or a Table's Name - Quad - Aug 2, 2019

There usually isn't a reason to give each user their own database, and my best guess is that you don't it either. Explain your problem in detail here, and we can give you a good database design, or look up some tutorials on relational database design.


RE: Is There Any Way to Randomly Generate A Database's or a Table's Name - Nachfolger - Aug 2, 2019

(Aug 2, 2019 09:50 AM)Quad Wrote:  There usually isn't a reason to give each user their own database, and my best guess is that you don't it either. Explain your problem in detail here, and we can give you a good database design, or look up some tutorials on relational database design.
This.

Either I missed your point OP, or there is not even a reason to randomly generate a database name. Reconsider your application design before it's too late.