views:

171

answers:

5

I have this code:

$link = mysql_connect("localhost", "ctmanager", "pswsafgcsadfgG");
if ( ! $link )
   die("I cannot connect to MySQL.<br>\n");
else
    print "Connection is established.<br>\n";
print "a";

if ( mysql_create_db("ct", $link) )
   print "AAA";
else
   print "BBB";
print "2";
die();

And this is the output:

Connection is established.
a

So, I cannot understand how it's possible that neither "AAA" no "BBB" is outputted. Is it because program dies at mysql_create_db?

+1  A: 

I think you're right it is failing at mysql_create_db but i would guess it's the SQL that you're passing into it. are you trying to create a DB called "ct"? If so i'd try "CREATE DATABASE ct"

used2could
+7  A: 

You probably guessed right. Try adding:

error_reporting(-1);
ini_set('display_errors', 'On');

at the very top of your script. You will get more details I'm sure. Post your findings and I'll update my answer if needed to.

Also, if you use PHP 5, you can try:

try
{
    if (mysql_create_db("ct", $link)) 
        echo 'AAA'; 
    else
        echo 'BBB'; 
}
catch (Exception $e)
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

maybe this will catch something...

Also, as František Žiačik pointed out with his link:

The function mysql_create_db() is deprecated. It is preferable to use mysql_query() to issue an sql CREATE DATABASE statement instead.

AlexV
+2  A: 

mysql_create_db is deprecated in PHP5, maybe there even does not exist anymore.

See http://php.net/manual/en/function.mysql-create-db.php for an example how to do it.

František Žiačik
The function mysql_create_db() is deprecated. It is preferable to use mysql_query() to issue an sql CREATE DATABASE statement instead.
AlexV
+1  A: 

What PHP version are you running? According to the documentation, mysql_create_db is depracated, and a CREATE DATABASE query should be issued instead.

if (mysql_query("CREATE DATABASE ct", $link))
  print "AAA";
else
  print "BBB";
Aistina
A: 

Hi,

I think this would be the alternate way hopefully.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
    echo "Database my_db created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error() . "\n";
}
?> 
Neeraj