I've recently been working on a fairly complicated game. I've stored information with local storage, but that allows the player to edit it, and does not transfer from computer to computer. The two scripts run on a single server, and I use the following to ping the php server:

function initiateLoginSequence() {
    var isnewplayer=prompt("Hello! Are you new?(Y/N)");
    if(isnewplayer.equalsIgnoreCase("N")) {
        var username = prompt("Enter username.");
        var password = prompt("Enter Password.");
        //parsing code goes here later.
    } else {
        var username = prompt("Enter a username. This will be used for future logins.");
        var password = prompt("Enter a password. This will be used for future logins.","*");
        var req = new XMLHttpRequest();  
        req.open('POST', '', true);   

I then process the results with this:

   if($returning=="false" && $save=="true") {
     $sqlcmd = "CREATE TABLE ".$username."(
     Username varchar(".$username.")
     Password varchar(".$password.")
     Score varchar(".$score.")
   } else if($save==false) {
     $sqlcmd="SELECT * FROM userdata"
     $result = mysql_query($sqlcmd);
     while($row=mysql_fetch_array($result)) {
        if($row['Username']==$username && $row['Password']) {
            echo row['Score'];
   } else if($save==true) {
     $sqlcmd="SELECT * FROM userdata"
     $result = mysql_query($sqlcmd);
     while($row=mysql_fetch_array($result)) {

But after creating the mysql database, nothing is populated. So my basic question is, Am I doing this right? If not, what am I missing?


2 Answers 2


Several points:

  • You should definitely enable error reporting and error logging into a file on the PHP side, otherwise you'll have no evidence of problems. AJAX errors usually cause little visible error to the user.
  • You should check for errors on the Javascript side too

PHP error handling is famous for being both bad (e.g. the default action for most unusual conditions is to continue regardless and issue a warning, which possibly goes to the client browser, or might be ignored as well), AND complicated. Once you fully understand PHP error handling, you can write an error handler which carries out appropriate actions in all cases.

In my opinion:

  • Creating a table for each user is not a good database model (Consider a row per user instead)
  • Hard-coding the server IP address in the Javascript code is almost certainly not the right thing to do (e.g, how do you move your application from your test to production server without changing it?)
  • a PHP error handler should log all errors and report every possible piece of contexual information (especially a stack trace) into the error log, and stop execution; it is necessary to provide a mechanism for certain errors to be suppressed in some cases (for example, error_reporting). Don't use the @ operator ever.


  • You may wish to research what SQL injection is, before you continue to use PHP / MySQL in your application (Hint: Consider using parameterised queries)
  • 3
    \$\begingroup\$ @dylanisawesome1 Definitely research what SQL injections are and how to prevent them. Your current code allows any user to do anything they want to your database, including adding, changing and deleting data of other users. \$\endgroup\$
    – Philipp
    Feb 11, 2013 at 13:13
  • 1
    \$\begingroup\$ Hard-coding the server IP address in the Javascript code is almost certainly not the right thing to do Actually this isn't necessarily a problem (eg. we do that in the game client I work on) but does involve defining both the dev AND production URLs and then having the server tell the game which environment it is in. So either do that or have the server actually inject the URL when serving up the game; either way, you need to have code on the server that tells the game how to initialize, something like point to init.php and have that generate the page for the game \$\endgroup\$
    – jhocking
    Feb 11, 2013 at 14:12
  • \$\begingroup\$ It's my opinion - it would be easier for the Javascript code to determine its own server address (i.e. its own server) than to try to distribute the right IP address in each case. Yours might be different. \$\endgroup\$
    – MarkR
    Feb 11, 2013 at 17:03

It seems to me like you have a misconception about what's the meaning of a TABLE and whats the meaning of a DATABASE in SQL.

A database has many tables.

Each table can have multiple columns and entries ("rows").

The SELECT statement gets data from a table of the currently active database. You are trying to select from userdata, which is the name of your database, not from a table of your database.

Your CREATE TABLE command is semanitically wrong. When you create a table, you create the columns. The columns define how each entry in the table looks, but no data is inserted yet. This happens later with separate INSERT INTO commands. The values in parenthesis after varchar is the length of the fields of the column. For example, username VARCHAR(20) means that each entry in the table has a field username which is a variable-length string with a length of up to 20 characters.

Also, you generally create the tables once when you deploy the application. When the application is in use, you avoid adding or changing tables. When you have multiple users, you usually create one table users where each row represents one user. The columns you create in the CREATE TABLE command (which is usually not done with PHP code but with a GUI tool like PhpMyAdmin) describe what data is saved in each row.


Not the answer you're looking for? Browse other questions tagged .