如何使用PHP和MySQL创建数据库?

时间:2012-05-14 13:10:28

标签: php mysql database create-table

在这个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);
            }
         }

3 个答案:

答案 0 :(得分:-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);
        }
     }

答案 1 :(得分:-2)

你没有在行中留出空间

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

答案 2 :(得分:-2)

简单,只需按照以下两个步骤进行操作

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

mysql_select_db("database_name", $con);