Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So i've assume you have only 1 MySQL database (minimum package on your webhost) and need to store a copy of a system for each of your clients.</p> <p>What I was suggesting, is that you create a separate set of tables as you already are (for each client), but the name wont matter because you have a look-up of the table names in your clients table.</p> <p>Heres my example for you: The clients table should store the table names of their own tables<br> (e.g. users_tbl = clientone_users for client id:1) So that later on you can just query the clients table and get his/her table names, then use that result to query on his/her user, news, pages, and files tables.</p> <pre><code># SQL: new table structure -- store the names of the clients tables here CREATE TABLE clients( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), name VARCHAR(50), address VARCHAR(250), email VARCHAR(50), pass BLOB, /* table names*/ users_tbl VARCHAR(70), news_tbl VARCHAR(70), pages_tbl VARCHAR(70), files_tbl VARCHAR(70) ) ENGINE = InnoDB; # PHP: Some definitions for the table structure $tbl_names = array("_users","_news","_pages","_files"); $tbl_fields = array(); $tbl_fields[0] = array("id INT","users_col1 VARCHAR(10)","users_col2 VARCHAR(20)"); $tbl_fields[1] = array("id INT","news_col1 DATE",...); $tbl_fields[2] = array(...); $tbl_fields[3] = array(...); // refers to YOUR clients table field names (see above) $clients_fields = array("users_tbl", "news_tbl", "pages_tbl", "files_tbl"); # PHP: Create a user and create the users database function createUser($name, $address, $email, $pass, $salt) { global $db, $tbl_names, $tbl_fields; $success = false; if ($db-&gt;beginTransaction()) { $sql = "INSERT INTO clients(name, address, email, pass) VALUES (?, ?, ?, AES_ENCRYPT(?, ?));" $query = $db-&gt;prepare($sql); $query-&gt;execute(array($name, $address, $email, $pass, $salt)); if ($query-&gt;rowCount() == 1) { # if rowCount() doesn't work # get the client ID # there are alternative ways $client_id = $db-&gt;lastInsertId(); for ($i=0; $i&lt;sizeof($tbl_names); $i++) { $client_tbl_name = $name . $tbl_names[$i]; $sql = "CREATE TABLE " . $client_tbl_name . "(" . implode(',', $tbl_fields[$i]) . ");"; if (!$db-&gt;query($sql)) { $db-&gt;rollBack(); return false; } else { $sql = "UPDATE clients SET ".clients_fields[$i]."=? " ."WHERE id=?;"; $query = $db-&gt;prepare($sql); if (!$query-&gt;execute( array($client_tbl_name, (int)$client_id) )) { $db-&gt;rollBack(); return false; } } } $db-&gt;commit(); $success = true; } if (!$success) $db-&gt;rollBack(); } return $success; } # PHP: Get the Client's table names function getClientsTableNames($client_id) { $sql = "SELECT (users_tbl, news_tbl, pages_tbl, files_tbl) FROM clients WHERE id=?;"; $query = $db-&gt;prepare($sql); if ($query-&gt;execute(array((int)$client_id))) return $query-&gt;fetchAll(); else return null; } # PHP: Use the table name to query it function getClientsTable($client_id, $table_no) { $table_names = getClientsTableNames($client_id); if ($table_names != null &amp;&amp; isset($table_names[$table_no])) { $sql = "SELECT * FROM ".$table_names[$table_no].";"; $query = $db-&gt;prepare($sql); if ($query-&gt;execute(array((int)$client_id))) return $query-&gt;fetchAll(); } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload