Create MySQL table

Discussion in 'PHP' started by sg552, Sep 5, 2008.

  1. #1
    Hello,

    How to create a mysql table from this php code. I'm super noobs in MySQL, sorry...

    // snippet of code to see if a user has downloaded too much
    
    $config['download_limit'] = 3;
    
    // check database for their IP
    $sql = "SELECT count(*) AS number FROM ".$db->pre."downloads
              WHERE ip='$_SERVER[REMOTE_ADDR]'
              AND category='games'";
    
    $iplocked = $db->query_first($sql);
    
    // check to see if IP and/or cookie is over the limit
    if($iplocked['number'] >= $config['download_limit'] || 
    $_COOKIE['download_limit'] >= $config['download_limit']){
        echo "over user limit error";
        // YOU: exit or redirect them
    }
    // they're good to go. allow download
    else{
        // insert their ip into database
        $data = array('ip'=>$_SERVER['REMOTE_ADDR'], 'time'=>time(), 'category'=>"games");
        $db->query_insert("downloads", $data); 
    
        // set a cookie too
        setcookie("download_limit", ($_COOKIE['download_limit']+1), time() + 24 * 3600);
    
        // YOU: send the user to the file
    }
    
    
    // deletes "expired" entries each time script is run
    $lockTime = time()-24*3600; // 24hrs
    $sql = "DELETE FROM ".$db->pre."downloads
              WHERE category='games'
              AND time < $lockTime";
    $db->query($sql); 
    PHP:
    I found this nice code from here: http://www.ricocheting.com/scripts/php_mysql_wrapper.php

    After 2 hour learning from tizag.com and after couple of try I give up :/


    
    <?php
    // Make a MySQL Connection
    mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    
    // Create a MySQL table in the selected database
    mysql_query("CREATE TABLE downloads(
    ip INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
     count INT), 
    category VARCHAR(30), 
     age INT)")
     or die(mysql_error());  
    
    echo "Table Created!";
    
    ?>
    PHP:
    Thanks in advance
    :)
     
    sg552, Sep 5, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    CREATE TABLE `downloads` (
      `ip` int(11) NOT NULL auto_increment,
      `category` varchar(30) NOT NULL,
      `age` int(11) NOT NULL,
      PRIMARY KEY  (`ip`)
    )
    Code (markup):
    That should be all you need to have for your create database. Also, note that there is no count since the SQL query in the first code you provided generates the count upon querying the database
     
    JAY6390, Sep 5, 2008 IP
  3. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    I try and run it in MySQL and I'm geting this error:

    Database Error 
    Message: MySQL Query fail: INSERT INTO `downloads` (`ip`, `time`, `category`) VALUES ('60.54.68.225', '1220677948', 'games'); 
    
    MySQL Error: Duplicate entry '60' for key 1 
    
    Date: Saturday, September 6, 2008 at 1:12:28 AM 
    
    Script: /demo/ 
    Code (markup):
    I think age is not needed...? Any idea?

    Thanks
    :)
     
    sg552, Sep 5, 2008 IP
  4. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    After a couple of try I come up with this and run it in phpmyadmin

    CREATE TABLE `downloads` (
      `ip` int(11) NOT NULL auto_increment,
      `time` TIME NOT NULL,
      `category` varchar(30) NOT NULL,
      PRIMARY KEY  (`ip`)
    )
    Code (markup):
    and now im getting this error:

    Database Error
    Message: Result ID: 1 could not be freed. 
    Date: Saturday, September 6, 2008 at 1:48:26 AM 
    Script: /demo/index.php 
    Code (markup):
    any idea? Thanks :)
     
    sg552, Sep 5, 2008 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    CREATE TABLE `downloads` (
      `id` int(11) auto_increment,
      `ip` varchar(15) NOT NULL,
      `category` varchar(30) NOT NULL,
      `time` TIME NOT NULL,
      PRIMARY KEY  (`id`)
    )
    Code (markup):
    Try that
     
    JAY6390, Sep 6, 2008 IP
    sg552 likes this.
  6. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,

    After run your code in my sql I'm still getting the same error:eek:

    [​IMG]
    [​IMG]

    anyway I will take a look at my code again and see anything wrong.

    Many thanks for your help
    :)
     
    sg552, Sep 6, 2008 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well I used the create table in a MySQL console, and also used the exact insert that you had...it works fine for me. Here are the screenshots
    Console
    [​IMG]

    Table contents
    [​IMG]
     
    JAY6390, Sep 6, 2008 IP
  8. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Hi,

    You use the code above without changing anything :confused:

    <? 
    // every page needs to start with these basic things
    
    // I'm using a separate config file. so pull in those values
    require("config.inc.php");
    
    // pull in the file with the database class
    require("database.class.php");
    
    // create the $db ojbect
    $db = new Database($config['server'], $config['user'], 
                       $config['pass'], $config['database'], $config['tablePrefix']);
    
    // connect to the server
    $db->connect();
    
    // snippet of code to see if a user has downloaded too much
    
    $config['download_limit'] = 3;
    
    // check database for their IP
    $sql = "SELECT count(*) AS number FROM ".$db->pre."downloads
              WHERE ip='$_SERVER[REMOTE_ADDR]'
              AND category='games'";
    
    $iplocked = $db->query_first($sql);
    
    // check to see if IP and/or cookie is over the limit
    if($iplocked['number'] >= $config['download_limit'] || 
    $_COOKIE['download_limit'] >= $config['download_limit']){
        echo "over user limit error";
        // YOU: exit or redirect them
    }
    // they're good to go. allow download
    else{
        // insert their ip into database
        $data = array('ip'=>$_SERVER['REMOTE_ADDR'], 'time'=>time(), 'category'=>"games");
        $db->query_insert("downloads", $data); 
    
        // set a cookie too
        setcookie("download_limit", ($_COOKIE['download_limit']+1), time() + 24 * 3600);
    
        // YOU: send the user to the file
    }
    
    
    // deletes "expired" entries each time script is run
    $lockTime = time()-24*3600; // 24hrs
    $sql = "DELETE FROM ".$db->pre."downloads
              WHERE category='games'
              AND time < $lockTime";
    $db->query($sql); 
    
    // and you're done, remember to close connection
    
    $db->close(); 
    
    ?>
    PHP:
     
    sg552, Sep 6, 2008 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No. I was just showing that the query for the table creation, and the insertion of multiple ip's that are the same work. Like it says above, it was done in a console window
     
    JAY6390, Sep 6, 2008 IP
  10. sg552

    sg552 Peon

    Messages:
    187
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thanks for the help + rep added :D
     
    sg552, Sep 10, 2008 IP
  11. softvision

    softvision Peon

    Messages:
    146
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0