-1

在这个create函数中,我试图创建一个数据库。但是,它无法创建并显示错误Error creating database: Access denied.因此,这意味着存在连接并且它通过了第一次检查。然后当mysql_query被调用时,它不会返回true。任何人都可以帮忙吗?

function create($dbName, $tbName, $fields, $types_sizes, $PK)
        {
            $sql = null;
            $con = mysql_connect("borg.cs.up.ac.za","username", "password");

            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            else 
                {
                  if(mysql_query("CREATE DATABASE". $dbName, $con))
                    {
                        echo "Database created";
                        $dbExists = 1;
                    }
                   else
                    {
                        echo "Error creating database: " . mysql_error();
                        $dbExists = 0;
                    }
                   $i = 0;
                    if(($con)&&($bdExists = 1))
                    {
                        $sql . "CREATE TABLE " . $tbName . "(";
                        while($i < count($fields))
                        {
                            $sql . $fields[$i] . " " + $types_sizes[$i] . ",";
                            $i++;
                        }
                    if($PK != null)
                    {
                        $sql . "PRIMARY KEY (" . $PK .")";
                    }
                    }
                mysql_query($sql,$con);
                mysql_close($con);
            }
         }
4

3 回答 3

-1

尝试这个。这是未经测试的。更改了代码中的一些行。

function create($dbName, $tbName, $fields, $types_sizes, $PK)
    {
        $sql = null;
        $con = mysql_connect("borg.cs.up.ac.za","username", "password");

        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        else 
            {
    if(mysql_select_db($dbName))
                {
                    echo "Database selected";
                    $dbExists = 1;
                }
               else
                {
                    echo "Error creating database: " . mysql_error();
                    $dbExists = 0;
                }
               $i = 0;
                if(($con)&&($dbExists == 1))
                {
                    $sql = "CREATE TABLE " . $tbName . "(";
                    while($i < count($fields))
                    {
                        $sql .= $fields[$i] . " " + $types_sizes[$i] . ",";
                        $i++;
                    }
                if($PK != null)
                {
                    $sql .= "PRIMARY KEY (" . $PK .")";
                }
                }
            mysql_query($sql,$con);
            mysql_close($con);
        }
     }
于 2012-05-14T13:24:34.907 回答
-1

你没有在行中给空间

if(mysql_query("CREATE DATABASE". $dbName, $con)),请在“创建数据库->创建数据库”之后提供空格.$dbname,否则它将是数据库+名称组合。

于 2012-05-14T13:19:18.013 回答
-2

很简单,只要按照这两个步骤

$con = mysql_connect("localhost", "root", "password");

mysql_select_db("database_name", $con);
于 2014-02-04T12:07:42.870 回答