开发者

Why it's not "if" and not "else"?

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 "B开发者_运维百科BB";
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?


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.


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.


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"


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";


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";
}
?> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜