SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast Dromenail's Avatar
    Join Date
    Nov 2006
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    null array keys?

    i have this code which i'm using to install my database:
    PHP Code:

        
    class install {
            public function 
    tableExist($table,$db) { 
                
    $this->tables mysql_list_tables($db); 
                while (list (
    $temp) = mysql_fetch_array ($this->tables)) {
                    if (
    $temp == $table) {
                    return 
    true;
                    }
                }
            return 
    false;
            }
            public function 
    __construct($db) {
                
    $tables2 = array(
                    
    "ld_users" => "CREATE TABLE ld_users (
                                        PRIMARY_KEY(id),
                                        id INT NOT NULL AUTO_INCREMENT,
                                        name VARCHAR(40) NOT NULL,
                                        username VARCHAR(30) NOT NULL,
                                        password VARCHAR(50) NOT NULL,
                                        avatar VARCHAR(100) NOT NULL,
                                        ip VARCHAR(20) NOT NULL,
                                        email VARCHAR(30) NOT NULL,
                                        website VARCHAR(30) NOT NULL,
                                        about VARCHAR(100) NOT NULL,
                                        payment VARCAR(20) NOT NULL
                                    )" 
                
    );
                foreach(
    $tables2 as $k => $v) {
                    if(!
    install::tableExist($tables2[$k],$db)) {
                        
    mysql_query($tables2[$v]) or die(query_err.mysql_error());
                    }
                }
            }
        } 
    and i'm getting this output:

    There was a query error. Details:
    Query was empty

    when i used var dump it told me that $v was null and it thought $k was the value. it may be obvious to someone else but i can't seem to see the problem :/ help please.
    Callum Carolan

  2. #2
    SitePoint Addict Beaumont's Avatar
    Join Date
    Mar 2005
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't your code be like this?

    PHP Code:
                foreach($tables2 as $k => $v) { 
                    if(!
    install::tableExist($k,$db)) { 
                        
    mysql_query($v) or die(query_err.mysql_error()); 
                    } 
                } 

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Why not check if the table exists in your query? It would reduce your db traffic by half.

    CREATE TABLE IF NOT EXISTS table_name ...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •