# MySQL Connection issue with script [ SOLVED ]

## flipnode

Okay, I've setup Apache2 and MySQL and all is working. I have setup phpMyAdmin and I'm able to login with root just fine. I even created another user and asigned him to a database. I write a php script to make a connection to Mysql to run a specific query that I want and it can't make a connection. The script is running locally, so I don't understand why it can't make a connection. I've checked the usernames and passwords and all is well when I login through phpmyadmin, but not through the script I wrote.

Here is the script

```

<?php

/*

 * This script is used to create a new Table in a specified Database.

 * /

$query = "CREATE TABLE news (

         NewsID int(5) NOT NULL auto_increment 

         Date date            

         Author varchar(20)      

         Title varchar(100)

         News blob

         PRIMARY KEY (NewsID)

      )";

function dbShowError()

{

   die( "Error:" . mysql_errno() . ":" . mysql_error() );

}

if( ! ( $dbConn = @mysql_connect( "localhost", "usernamehere", "passwordhere" ) ) );

   die( "Could not connect" );

   

if( ! ( @$mysql_select_db( "ap", $dbConn ) ) );

   dbShowError();

if( ! ( @$mysql_query( $query, $dbConn ) ) )

   dbShowError();

else

   print "News Table was created, you can now write scripts to post news";

?>

```

----------

## flipnode

okay, so I tried everything and I finally decided that it had to be my script in some way. So, I tried a very basic script off of the php.net site.

```

$link = mysql_connect('localhost', 'flipnode', '18worm');

if (!$link) {

   die('Could not connect: ' . mysql_error());

}

echo 'Connected successfully';

mysql_close($link);

```

and it seemed to work. Why the hell?? Anyone?

----------

## kashani

Does your user have access to create tables? You can check by doing 

```

mysql -u root -p

use mysql;

select * from db where user='username';

```

You'd need create privileges to create a table within the db.

kashani

----------

## flipnode

Yes, the user has permission. I setup the user permissions through phpmyadmin. I believe the issue was my script. If you notice, if statements have ';' at the end. I believe that was causing the error. 

I'm an idiot!

Here is the fixed script that works

```

<?php

$sqlQuery = 'CREATE TABLE news '

         . '('

           . ' NewsID int(5) NOT NULL auto_increment ,'

          . ' Date date,'

         . ' Author varchar(20),'

         . ' Title varchar(100),'

         . ' News blob,'

         . ' PRIMARY KEY (NewsID)'

         . ')';

function dbShowError()

{

   die( "Error: [" . mysql_errno() . "] : " . mysql_error() );

}

if( ! ( $dbConnection = @mysql_connect( 'localhost', 'username', 'password' ) ) )

{

   die( "Could not connect" );

}

if( ! @mysql_select_db( 'ap', $dbConnection ) )

{

   dbShowError();

}

if( ! @mysql_query( $sqlQuery, $dbConnection ) )

{

   dbShowError();

}

else

{

   print "News Table was created, you can now write scripts to post news";

}

$un_set( $sqlQuery );

$mysql_close( $dbConnection );

?>

```

----------

