Results 1 to 11 of 11

Thread: Creating databases using PHP?

  1. #1
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts

    Creating databases using PHP?

    Im trying to use PHP to create a database and its tables and it just aint having it! can anyone tell me wat im doing wrong?

    Error:
    --------------------------------------------------------------------------------
    Warning: Unexpected character in input: ' ' (ASCII=12) state=1 in /home/nemelize/public_html/appy/dbsetup.php on line 46

    Warning: Unexpected character in input: ' ' (ASCII=12) state=1 in /home/nemelize/public_html/appy/dbsetup.php on line 54

    Warning: Unexpected character in input: ' ' (ASCII=12) state=1 in /home/nemelize/public_html/appy/dbsetup.php on line 62

    Warning: Unexpected character in input: ' ' (ASCII=12) state=1 in /home/nemelize/public_html/appy/dbsetup.php on line 70
    Access denied for user: '@localhost' to database 'nemelize_nemelization'
    --------------------------------------------------------------------------------

    Heres my code: I have numbered the lines to make it easier to read.
    PHP Code:
    32. $dbname "nemelize_nemelization";
    33. $sites_tablename 'sites';
    34. $sites_table_def "SiteID VARCHAR(4) BINARY NOT NULL AUTO_INCREMENT, ";
    35. $sites_table_def .= "SiteTitle TEXT NOT NULL, ";
    36. $sites_table_def .= "SiteDescription TEXT NOT NULL, ";
    37. $sites_table_def .= "SiteImage VARCHAR(200) NOT NULL, ";
    38. $sites_table_def .= "SiteURL VARCHAR(300)NOT NULL, ";
    39. $sites_table_def .= "PRIMARY KEY (SiteID)";

    40. $logos_tablename 'logos';
    41. $logos_table_def "LogoID VARCHAR(4) BINARY NOT NULL AUTO_INCREMENT, ";
    42. $logos_table_def .= "LogoTitle TEXT NOT NULL, ";
    43. $logos_table_def .= "LogoDescription TEXT NOT NULL, ";
    44. $logos_table_def .= "LogoImage VARCHAR(200) NOT NULL, ";
    45. $logos_table_def .= "LogoURL VARCHAR(300)NOT NULL, ";
    46. $logos_table_def .= "PRIMARY KEY (LogoID)";

    47. $other_tablename 'other';
    48. $other_table_def "OtherID VARCHAR(4) BINARY NOT NULL AUTO_INCREMENT, ";
    49. $other_table_def .= "OtherTitle TEXT NOT NULL, ";
    50. $other_table_def .= "OtherDescription TEXT NOT NULL, ";
    51. $other_table_def .= "OtherImage VARCHAR(200) NOT NULL, ";
    52. $other_table_def .= "OtherURL VARCHAR(300)NOT NULL, ";
    53. $other_table_def .= "PRIMARY KEY (OtherID)";

    54. $featured_tablename 'features';
    55. $featured_table_def "FeaturedID VARCHAR(4) BINARY NOT NULL AUTO_INCREMENT, ";
    56. $featured_table_def .= "FeaturedTitle TEXT NOT NULL, ";
    57. $featured_table_def .= "FeaturedDescription TEXT NOT NULL, ";
    58. $featured_table_def .= "FeaturedImage VARCHAR(200) NOT NULL, ";
    59. $featured_table_def .= "FeaturedURL VARCHAR(300)NOT NULL, ";
    60. $featured_table_def .= "PRIMARY KEY (FeaturedID)";

    61. $link_id db_connect();
    62. if(!$link_id) die(mysql_error());

    63. if(!mysql_query("CREATE DATABASE $dbname")) dir(sql_error());
    64.     echo "Succesfully created database $dbname. <BR>";

    65. if(!mysql_select_db($dbname)) die(dql_error());

    66. if(!mysql_query("CREATE TABLE $sites_tablename ($sites_tables_def)")) die(sql_error());

    67. if(!mysql_query("CREATE TABLE $logos_tablename ($logos_tables_def)")) die(sql_error());

    68. if(!mysql_query("CREATE TABLE $other_tablename ($other_tables_def)")) die(sql_error());

    69. if(!mysql_query("CREATE TABLE $featured_tablename ($featured_tables_def)")) die(sql_error());


    70. echo "Successfully created the $sites_tablename$logos_tablename$other_tablename and $featured_tablename tables.";
    71.  ?> 
    Help

  2. #2
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    <?php

    $db_con = mysql_connect(localhost,username,password) or die("Database is fux0red.");

    mysql_select_db(database_name,$db_con) or die("Unable to select database.");

    if(mysql_query("IF EXISTS " . tutorials)) echo "Table tutorials already exists.";
    else
    {
    $query = "CREATE TABLE tutorials (
    id int(3) DEFAULT '0' NOT NULL auto_increment,
    title varchar(50),
    category varchar(12),
    author varchar(20),
    date varchar(12),
    content text,
    PRIMARY KEY (id))";

    if(mysql_query($query)) echo "Table tutorials created.<br>";
    else{
    $mysql[error] = mysql_error();
    echo "$mysql[error]";
    }
    }
    ?>



    I couldn't see you connecting to the db or selecting it. Have a look at my way of doing it. This way definitely works as long as you get the username and password right

    That's the template i use for all tables that go in my site. Also is your database actually there?

  3. #3
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    You dont appear to have a user name or password there. To do this you will need to get one. Talk to your host, should be in control panel somewhere.

    or you could get phpmyadmin that allows you to do loads of stuff through a webpage (will still need to know username and password to set this up though)
    Twigman

  4. #4
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    Jersey UK?! you should have it as Jersey CI

  5. #5
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts
    Hey

    This is the part that comes before that piece of code?:

    PHP Code:
     2. $dbhost 'localhost';
     
    3. $dbusername 'DATABASEUSERNAME';
     
    4. $dbuserpassword 'DATABASEPASSWORD';
     
    5. $default_dbname 'DATABASENAME';

     
    6. $MYSQL_ERRNO '';
     
    7. $MYSQL_ERROR '';

     
    8. function db_connect() {
     
    9.     global $dbhost$dbusername$dbpassword$default_dbname;
    10.     global $MYSQL_ERRNO$MYSQL_ERROR;

    11.     $link_id mysql_connect($dbhost$dbuser$dbuserpassword);
    12.     if(!$link_id) {
    13.         $MYSQL_ERRNO 0;
    14.         $MYSQL_ERROR "Connection failed to the host $dbhost.";
    15.         return 0;
    16.     }
    17.     else if(empty($dbname) && !mysql_select_db($default_dbname)) {
    18.         $MYSQL_ERRNO mysql_errno();
    19.         $MYSQL_ERROR mysql_error();
    20.         return 0;
    21.     }
    22.     else return $link_id;
    23. }

    24. function sql_error() {
    25.     global $MYSQL_ERRNO$MYSQL_ERROR;

    26.     if(empty ($MYSQL_ERROR)) {
    27.         $MYSQL_ERRNO mysql_errno();
    28.         $MYSQL_ERROR mysql_error();
    29.     }
    30.     return "$MYSQL_ERRNO$MYSQL_ERROR";
    31. 
    I left this part out to save space on my last post

  6. #6
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    ffs silly guernsey people always have to run in there and spoil it dont they. Its for the dumn americans who dont have a clue. Apologies to them, but the amount of time people assume I mean New Jersey...ARGH! did I say new? no! so shut up!...lol
    Twigman

  7. #7
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    heh, i would like to know why there is rivalry between the islands although guernsey does own every single island except for jersey and jersey is just by itself so......

  8. #8
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    We are bigger than you...we shall 0wn j00!
    I was going to organise a CS match with some people in Guernsey but they were all bitching about not having adsl when we did, so i left it, now you do have ADSL I dont play cs any more...
    Twigman

  9. #9
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    heh, yeh but it's crap. ADSL service over here is absolutely terrible atm. Anyway, this doesn't belong in this thread. :/

  10. #10
    Senior Member
    Join Date
    Oct 2003
    Posts
    2,069
    Thanks
    4
    Thanked
    7 times in 3 posts
    yeah well look who started it...lol

    back on topic:
    did you code that yourself?
    I would just replace it all with whats shown above. That works and is much more consise etc...
    Twigman

  11. #11
    Senior Member Nemeliza's Avatar
    Join Date
    Jul 2003
    Posts
    1,719
    Thanks
    1
    Thanked
    5 times in 5 posts
    got it to work in the end. dun kno how but its working =/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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