如何使用PHP MySql建立与数据库的连接

时间:2013-01-28 07:13:35

标签: php html mysql

以下代码在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中检查已创建的数据库,则表示未创建数据库。代码有什么问题?我怎么能即兴创作呢?我不能在里面使用create database命令

5 个答案:

答案 0 :(得分:3)

你刚才有创建数据库的书面查询没有执行it.replace代码下面的代码

$sql = "CREATE DATABASE db1";
mysql_query($sql, $con);
mysql_select_db("db1", $con);

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

答案 2 :(得分: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;
?>

免责声明:错误处理,检查,输入卫生设施已简化过程。

答案 3 :(得分:0)

改变:

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

mysql_select_db("db1", $con);

它应该是有效的。它将使用db1名称创建数据库,并在其中创建表年。

答案 4 :(得分:0)

您还应该执行查询:

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

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