Page 1 of 1

Converting php script from mysql to sqlite

Posted: Tue Dec 21, 2004 11:34 am
by Termina
I have a script that adds users into a mysql database for a game.

I found an updated version of the game, but it uses sqlite for some reason.

Could anyone help me convert it to sqlite? Thanks!

Here's the script I'm talking about:

Update: by the way, I use php4

Code: Select all

<?
   /* author: Adam Lindquist; written: Jan  5, 2004
      modded: Adam Lindquist;          Jan 24, 2004
              desc: made the script more modular
      description: adds account data to the database */

   // this adds the html structure of the site
   include("layout.php");

   /* desc: checks the database for duplicate account names
      ret: duplicate if found
      ret: false if no duplicates */
   function check_duplicates($link, $name, $email) &#123;
     /* select all names, and emails from the account database
        that equal the values the user wants to use */
     $query = "SELECT accname, email FROM accounts
               WHERE accname = '$name' OR email = '$email'";

     $result = mysql_query($query, $link) or die("No accounts to query");

     // if no accounts are returned there are no duplicates
     if (mysql_num_rows($result) == 0) return FALSE;

     // find the duplicate by searching fields
     $duplicates = mysql_fetch_array($result, MYSQL_ASSOC);

     // were done with recordset so close it
     mysql_free_result($result);

     /* search the duplicates. uses foreach on the event that duplicates
        turns up more than one record (i.e. duplicate email in one record
        and a duplicate name in another) */
     foreach ($duplicates as $value) &#123;

       // returns the duplicate if a match is found
       if ($value == $name)
          return $name;
       elseif ($value == $email)
          return $email;
     &#125;
   &#125;

   // desc: outputs an error the the browser
   function generate_error($error) &#123;

     echo('<center>Sorry! There was an error...<br/><hr/>');
     echo($error);
     echo('<form action="'.$_SERVER&#1111;"SCRIPT_NAME"].'" method="post">');
     echo('<input type="submit" value="<< Back" />');
     echo('</form></center>');

     // kill the script so it will not continue and generate the form
     die();
   &#125;

   // desc: outputs a custom message after a successful execution
   function report_success($arg) &#123;

     echo(str_replace("#name#", $arg, file_get_contents("success_join.txt")));

     // kill the script so it will not continue and generate the form
     die();
   &#125;

   function add_page_content() &#123;
    /* include your script here and it will be added
       to the content cell in the html layout */

    $cmd = ( empty($_POST&#1111;"method"]) ? "" : trim($_POST&#1111;"method"]) );

    // if we get this value then execute the script!
    if ($cmd == "execute") &#123;

       // grab our form data
       if (!empty($_POST&#1111;"account"]))  $name     = trim($_POST&#1111;"account"]);
       if (!empty($_POST&#1111;"email"]))    $email    = trim($_POST&#1111;"email"]);
       if (!empty($_POST&#1111;"password"])) $password = trim($_POST&#1111;"password"]);

       /* check that the data send is all valid otherwise generate
          an error to the user */
       if (empty($name)) &#123;
         generate_error("You forgot to specify an account name.");
       &#125;
       elseif (empty($password)) &#123;
         generate_error("You forgot to specify a password.");
       &#125;
       elseif (empty($email)) &#123;
         generate_error("You forgot to specify an email.");
       &#125;
       elseif (strpos($email, "@") === false) &#123;
         generate_error($email." is not a valid email. Please use a valid one, we may need
                        to contact you with it.");
       &#125;

       // encrypt the player's password
       $str = "/home/will/openserver2/encrypt ".$password;
       $encrpass = exec($str, $ret_val);

       // connect to the account database
       $link = mysql_connect("art2oo0.kicks-ass.org", "root", "pass")
               or die("Could not connect to database");

       mysql_select_db("openserver", $link)
               or die("Could not select database");

       if (($ret = check_duplicates($link, $name, $email)) == false) &#123;

         // if no duplicates were found we can add the new account information
         $query = "INSERT INTO accounts (accname, encrpass, email)
                   VALUES ('$name', '$encrpass', '$email')";

         mysql_query($query, $link) or die("Unable to insert account data.");

         // clean up our resources
         mysql_free_result($result);
         mysql_close($link);

         report_success($name);
       &#125;
       else 
         generate_error($ret." already is in use, please use a different one.");
    &#125;

    // default account sign-up form generated below ?>
<center>
<body bgcolor="#000000">
<body text="#33CC00">
<form action="<?= $_SERVER&#1111;"SCRIPT_NAME"] ?>" method="post">
<input type="hidden" name="method" value="execute" />
<table cellpadding="3" cellspacing="0">
<tr><td colspan="2"><B>By joining, you agree to the Terms and Conditions of this site</B></td></tr>
<tr><td>Name :</td><td><input type="text" name="account" /></td></tr>
<tr><td>Password :</td><td><input type="password" name="password" /></td></tr>
<tr><td>Email :</td><td><input type="text" name="email" /></td></tr>
<tr><td colspan="2"><input type="submit" value="Add my Account!" /></td></tr>
</table>
</form>
</center>
<? &#125; // end of join.php content script ?>