为什么它不是"如果"而不是"其他"?

Rom*_*man 0 php if-statement

我有这个代码:

$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();
Run Code Online (Sandbox Code Playgroud)

这是输出:

Connection is established.
a
Run Code Online (Sandbox Code Playgroud)

所以,我无法理解输出"AAA"和"BBB"的可能性如何.是因为程序死了mysql_create_db吗?

Ale*_*exV 8

你可能猜对了.尝试添加:

error_reporting(-1);
ini_set('display_errors', 'On');
Run Code Online (Sandbox Code Playgroud)

在脚本的最顶层.你肯定会得到更多细节.发布您的发现,如果需要,我会更新我的答案.

此外,如果您使用PHP 5,您可以尝试:

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

也许这会抓住一些东西......

?此外,作为FRANTISEK齐亚IK指出了他的链接:

不推荐使用函数mysql_create_db().最好使用mysql_query()来发出sql CREATE DATABASE语句.