Visualizzazione dei risultati da 1 a 4 su 4

Discussione: Creazione tabelle

  1. #1

    Creazione tabelle

    Sto creando una tabella con PHP e usando una classe.

    Questa è la classe:

    Codice PHP:
    <?
    /*||||||||||||||||||||||||||||||||*\
    ||                                ||
    ||  ############################  ||
    ||  #    Simple Query Class    #  ||
    ||  ############################  ||
    ||  #        By Alefux         #  ||
    ||  ############################  ||
    ||  # alefux[at]gmail[dot]com  #  ||
    ||  ############################  ||
    ||                                ||
    \*||||||||||||||||||||||||||||||||*/

    /*

    Nome Classe: SimpleQuery
    Autore: Alefux <alefux@gmail.com>

    Versione: 1.00 

    Rilasciato sotto licenza CopyLeft
    ___________________________________

    Installazione:
    Incolla questo codice dentro un file php
    e includilo in ogni script in cui vorrai usarlo
    ___________________________________

    Modalità D'uso:

    Inizializza la classe:
    $myvar = new sq;

    Connettiti e seleziona il database:
    $myvar->connetti("host","user","password","database"); 

    Esegui una query:
    $query = $myvar->query("SELECT * INTO tabella WHERE autorediquestoscript = Alefux"); 

    Preleva la prima riga del risultato della query:
    $result=$myvar->result("SELECT * INTO tabella WHERE autore_di_questa_classe = Alefux"); 

    Preleva tutte le righe del risultato della query:
    $query=$ph->query("SELECT * INTO tabella WHERE autore_di_questa_classe = Alefux"); //definisci la query
    while($result=$ph->fetch($query)) { 
    // Fai ciò che vuoi con tutti i risultati

    $ph->free($query); // libera la variabile

    Ottieni il numero di righe del risultato di una query: 
    $query=$ph->query("SELECT * INTO tabella WHERE autore_di_questa_classe = Alefux"); 
    $rows=$ph->rows($query);  

    Ottieni l'ultimo id inserito: 
    $last_id=$ph->last_id(); 

    Disconnettiti da mysql: 
    $ph->disconnetti();  

    ***********************************/
    class sq {

        function 
    connetti($host="localhost",$user,$pass,$data,$persisti=false) {
            
    $this->host $host;
            
    $this->user $user;
            
    $this->pass $pass;
            
    $this->data $data;
            
    $this->persisti $persisti;
            
            if(
    $this->persisti) {
                
    $this->link=mysql_pconnect($this->host,$this->user,$this->pass);
            } else {
                
    $this->link=mysql_connect($this->host,$this->user,$this->pass);
            }
            
            if((
    $this->link) && ($this->data)) {
                if(
    mysql_select_db($this->data,$this->link)) {
                    return 
    True;
                }
            }
            return 
    False;
        }

        function 
    fetch($query="") {
            
    $query=mysql_fetch_array($query);
            return(
    $query);
        }
        
        function 
    query($query="",$sup="") {
            if(
    $sup) {
                
    $query=mysql_query($query,$this->link);
            } else {
                
    $query mysql_query($query,$this->link) or die("

    "
    .mysql_error()."

    "
    );
            }
            return(
    $query);
        }
        
        function 
    rows($query="") {
            
    $query=mysql_num_rows($query);
            return(
    $query);
        }
        function 
    free($query="") {
            
    mysql_free_result($query);
        }
        function 
    result($query="",$sup="") {
            
    $query=$this->query($query,$sup);
            
    $result=$this->fetch($query);
            
    $this->free($query);
            return(
    $result);
        }
        function 
    disconnetti() {
            
    mysql_close($this->link);
        }
        
    /* By PTM */
        
    function __destruct(){
            
    mysql_close($this->link);
        }  
        
    /* Fine idea di PTM */
    }
    ?>
    E questo è il file d'installazione
    Codice PHP:
    <?php
    include("inc/config.php");

    if (! 
    $_GET['step']) {
    echo 
    "Benvenuto nell'installazione di //////, clicca sul link seguente per avviare l'installazione guidata: <a href=\"install.php?step=1\">vai</a>.";
    }
    if (
    $_GET['step'] == 1) {
    echo 
    "<html><head><title>Installazione - Step 1</title></head>";
    echo 
    "<body>";
    echo 
    "<h1>[b]I dati inseriti nel config.php[/b]</h1>

    "
    ;
    echo 
    "[b]Host:[/b] ".$host."
    "
    ;
    echo 
    "[b]Username:[/b] ".$userdb."
    "
    ;
    echo 
    "[b]Password:[/b] ".$passdb."
    "
    ;
    echo 
    "[b]Database:[/b] ".$database."
    "
    ;
    echo 
    "Se i dati che hai inserito sono corretti clicca sul link seguente";
    echo 
    " <a href=\"install.php?step=2\">Step successivo</a>";
    echo 
    "</body></html>";
    }
    if (
    $_GET['step'] == 2) {
    echo 
    "<html><head><title>Installazione - Step 2</title></head>";
    echo 
    "<body>";
    echo 
    "Creazione tabelle in corso...
    "
    ;
    $query "CREATE TABLE `users` (`id` INT(11) NOT NULL AUTO_INCREMENT, `username` TEXT(32) NOT NULL, `password` TEXT(32) NOT NULL, `email` VARCHAR NOT NULL, `npost` INT(6) NOT NULL, `nthread` INT(30) NOT NULL, `groups` INT(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM";
    $sql $db->query($query);
    $query "CREATE TABLE `forums` (`id` INT(11) NOT NULL AUTO_INCREMENT, `name` TEXT(32) NOT NULL, `description` TEXT(60) NOT NULL, `father` TEXT(32) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM";
    $sql $db->query($query);
    $query "CREATE TABLE `threads` (`id` INT(11) NOT NULL AUTO_INCREMENT, `title` TEXT(32) NOT NULL, `message` TEXT(300) NOT NULL, `author` TEXT(32) NOT NULL, `dateinv` DATE NOT NULL, `replies` INT(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM";
    $sql $db->query($query);
    $query "CREATE TABLE `posts` (`id` INT(11) NOT NULL AUTO_INCREMENT, `title` TEXT(32) NOT NULL, `message` TEXT(300) NOT NULL, `author` TEXT(32) NOT NULL, `dateinv` DATE NOT NULL, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM";
    $sql $db->query($query);
    echo 
    "
    Creazione tabelle avvenuta con successo, clicca sul link seguente per passare allo step successivo: <a href=\"install.php?step=3\">vai</a>"
    ;
    echo 
    "</body></html>";
    }
    if (
    $_GET['step'] == 3) {
    echo 
    "<html><head><title>Installazione - Step 3</title></head>";
    echo 
    "<body>";
    echo 
    "<h1>[b]Account SuperAmministratore[/b]</h1>

    "
    ;
    echo 
    "<form action=\"\" method=\"POST\">";
    echo 
    "Username: <input type=\"text\" name=\"username\">
    "
    ;
    echo 
    "Password: <input type=\"password\" name=\"password\">
    "
    ;
    echo 
    "Email: <input type=\"text\" name=\"email\">
    "
    ;
    echo 
    "<input type=\"submit\" value=\"Registra utente e vai al passaggio successivo\"></form>
    "
    ;
    if (
    htmlspecialchars($_POST['username']) == "" OR htmlspecialchars($_POST['password']) == "" OR htmlspecialchars($_POST['email']) == "") {
    echo 
    "[b]<font color=\"#FF0000\">Compila tutti i campi</font>[/b]";
    } else {
    $admusername htmlspecialchars($_POST['username']);
    $admpassword htmlspecialchars($_POST['password']);
    $admemail htmlspecialchars($_POST['email']);
    $query "INSERT INTO users (id, username, password, email, npost, nthread, groups) VALUES (NULL, '$admusername', '$admpassword', '$admemail', 0, 0, 4)" or die(mysql_error());
    $sql $db->query($query); 
    echo 
    "[b]Installazione terminata. Al fine di prevenire danni al proprio host si consiglia di eliminare il seguente file. Per visualizzare la tua copia di ///// vai <a href=\"index.php\">qui</a>";
    echo 
    "</body></html>";
    }
    }
    ?>
    Il problema è che creando la tabella users PHP mi restituisce questo errore:
    codice:
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL, `npost` INT(6) NOT NULL, `nthread` INT(30) NOT NULL, `groups` INT(11) ' at line 1
    Come posso risolvere?

  2. #2
    Ricontrolla la query. Probabilmente c'è un errore di sintassi

  3. #3
    Utente di HTML.it L'avatar di nicola75ss
    Registrato dal
    Nov 2004
    Messaggi
    12,922
    Vedo un varchar in cui non specifichi la dimensione.
    Poi ci sarebbero altre cose, tipo l'id che essendo chiave primaria è già univoca di suo e quindi non necessita dell'attributo unique, l'utilizzo di campi text per nome utente e password. Insomma, rivediti un pò il tutto.

  4. #4
    Utente di HTML.it L'avatar di echoweb
    Registrato dal
    Sep 2008
    Messaggi
    419
    Ho provato a testare la query

    Codice PHP:
    $query "CREATE TABLE `users` (`id` INT(11) NOT NULL AUTO_INCREMENT, `username` TEXT(32) NOT NULL, `password` TEXT(32) NOT NULL, `email` VARCHAR NOT NULL, `npost` INT(6) NOT NULL, `nthread` INT(30) NOT NULL, `groups` INT(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM"
    e mi da lo stesso errore.

    Per risolverlo è bastato dare una dimensione al campo email come ti suggerisce nicola75ss.

    Modifica con

    Codice PHP:
    $query "CREATE TABLE `users` (`id` INT(11) NOT NULL AUTO_INCREMENT, `username` TEXT(32) NOT NULL, `password` TEXT(32) NOT NULL, `email` VARCHAR(50) NOT NULL, `npost` INT(6) NOT NULL, `nthread` INT(30) NOT NULL, `groups` INT(11) NOT NULL, PRIMARY KEY (`id`), UNIQUE (`id`)) ENGINE = MyISAM"


    "Non soffocare la tua ispirazione e la tua immaginazione,
    non diventare lo schiavo del tuo modello"

    Vincent van Gogh

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.