0

以下代码在php中创建数据库是否正确?

<?php

    $con=mysql_connect('localhost','admin','admin');
    if(!$con)
    {
        die("could not connect:' .mysql_error());
    }


    $sql = "CREATE DATABASE db1";
    mysql_select_db("db1", $con);
    $sql = "CREATE TABLE year
    (
        ayear varchar(10),
        fyear varchar(10)

    )";
    if(isset($_POST['id'])) 
    { 
        $ayear = $_POST['ayear'];
        $fyear = $_POST['fyear'];
        if($ayear != "" && $fyear != "") 
        {
            $query = "INSERT INTO year VALUES ('$ayear', '$fyear')"; 
            $result = mysql_query($query) or die(mysql_error());
        }
        else 
            echo "one of the field is empty";
     } 
    if (!mysql_query($sql,$con))
    {
         die('Error: ' . mysql_error()); 
    }
    mysql_close($con); 

?> 

执行代码后,如果我在 MySql 中检查已创建的数据库,则未创建数据库。代码有什么问题?我该如何即兴创作?我不能在里面使用创建数据库命令吗

4

5 回答 5

3

您刚刚编写了创建数据库的查询没有执行它。用您的代码替换下面的代码

$sql = "CREATE DATABASE db1";
mysql_query($sql, $con);
mysql_select_db("db1", $con);
于 2013-01-28T07:19:13.087 回答
1

试试这个 -

$sql = "CREATE DATABASE db1";
if (mysql_query($sql, $con)) {
    echo "Database db1 created successfully\n";
} else {
    echo 'Error creating database: ' . mysql_error();
}
mysql_select_db("db1", $con);
于 2013-01-28T07:22:51.203 回答
1

只是为了多样性,因为我们经常在这里说最好开始使用 PDO 而不是 mysql_* 这你可以如何使用 PDO

<?php
//Connect to mysql, omit db name since we want to check if db exist
$db = new PDO('mysql:host=localhost;charset=UTF-8', 'user', 'password');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

//Create db if it doesn't exist
$sql = "CREATE DATABASE IF NOT EXISTS db1";
$db->exec($sql);
//Select db since we didn't provide that information in DSN
$sql = "USE db1";
$db->exec($sql);
//Create table if doesn't exist
$sql = "CREATE TABLE IF NOT EXISTS `year`
        (ayear varchar(10), fyear varchar(10))";
$db->exec($sql);

//Don't forget to include all your necessary checks somewhere here
if(isset($_POST['id']) && 
   isset($_POST['ayear']) && $_POST['ayear'] && 
   isset($_POST['fyear']) && $_POST['fyear']) { 

    //Don't forget to include the code to check and sanitize user's input
    $ayear = $_POST['ayear'];
    $fyear = $_POST['fyear'];

    //Insert data using prepared statement
    $query = $db->prepare("INSERT INTO `year` VALUES (:ayear, :fyear)"); 
    $query->execute(array(':ayear' => $ayear, ':fyear' => $fyear));

    //Select data from the table
    $sql = "SELECT * FROM `year`";
    foreach ($db->query($sql) as $row) {
        echo "ayear: " . $row['ayear'] . "  fyear: " . $row['fyear'] . "<br>";
    }
} else {
    echo "One of the field is empty.";
}
//Close the connection to the db
$db = null;
?>

免责声明:为简洁起见,跳过了错误处理、检查、输入卫生。

于 2013-01-28T08:19:33.327 回答
0

进行了更改:

$sql = "CREATE DATABASE db1";
$result = mysql_query($sql);

mysql_select_db("db1", $con);

它应该是工作。它将使用 db1 名称创建数据库,并在其中创建表 year。

于 2013-01-28T07:56:55.193 回答
0

您还应该执行查询:

$sql = "CREATE DATABASE db1";
mysql_query($sql);

然后,您的数据库将被成功创建。

于 2013-11-30T17:26:20.017 回答