Can't Use PHP to create table in MySQL

Discussion in 'MySQL' started by jawinn, Oct 1, 2006.

  1. #1
    I'm trying to connect to a Godaddy MySQL DB using PHP. This is not a remote connection. This is on a shared account. Here is the code I'm trying to use. I have to be missing something.

    <?php

    // set your infomation.
    $hostname='server.servername.net';
    $username='name';
    $password='password';
    $dbname='dbname';

    // connect to the mysql database server.
    $link_id = mysql_connect ($hostname, $username, $password);
    echo "success in database connection.";

    // select the specific database name we want to access.
    $dbname=$username."_".$dbname;
    if (!mysql_select_db($dbname)) die(mysql_error());
    echo "success in database selection.";

    // add a table to the selected database
    $result="CREATE TABLE address_book (first_name VARCHAR(25), last_name VARCHAR(25), phone_number VARCHAR(15))";
    if (mysql_query($result)){
    echo "success in table creation.";
    } else {
    echo "no table created.";
    }

    ?>

    I keep getting in the browser:
    success in database connection.Access denied for user: 'name@%' to database 'dbname_dbname'

    Any help is much appreciated.
     
    jawinn, Oct 1, 2006 IP
  2. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #2
    figured it out, nevermind guys
     
    jawinn, Oct 1, 2006 IP
  3. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #3
    it would be nice if u also post a solution , or ask a mod to delete it ...
     
    commandos, Oct 1, 2006 IP
  4. jawinn

    jawinn Active Member

    Messages:
    1,024
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    88
    #4
    Sure, this is what worked:

    <?php

    $handle = mysql_connect('server', 'DB1000','password');

    echo ($handle) ? "Connected to MySQL.\r\n" : "Could not connect to MySQL.\r\n";

    mysql_select_db('DB1000') or die('Cannot select database');

    $query = 'CREATE TABLE tables( '.
    'cid INT NOT NULL AUTO_INCREMENT, '.
    'fname VARCHAR(20) NOT NULL, '.
    'lname VARCHAR(20) NOT NULL, '.
    'city VARCHAR(50) NOT NULL, '.
    'state VARCHAR(30) NOT NULL, '.
    'email VARCHAR (50) NOT NULL, '.
    'answer CHAR (5) NOT NULL, '.
    'PRIMARY KEY(cid))';

    $result = mysql_query($query);

    mysql_close($handle);

    ?>
     
    jawinn, Oct 2, 2006 IP
  5. intoex

    intoex Peon

    Messages:
    414
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Database user didn't have access to create tables, right?
     
    intoex, Oct 7, 2006 IP