首页 > 代码库 > php连接数据库、创建数据库、创建数据表

php连接数据库、创建数据库、创建数据表

<?php
    $con = mysql_connect("localhost", "root", "root");
    if(!$con){
        die("Could not connection:" . mysql_error());
    }else{
        echo $con;
    }
    
    /*$dbName = $_GET[‘databaseName‘];
    if($dbName && isset($dbName)){
        createDatabase();
    }*/
    $tableName = $_GET[‘tableName‘];
    if($tableName && isset($tableName)){
        createTable_PERSON();
    }
    
    
    function createDatabase(){
        echo "创建数据库中";
        $dbName = $_GET[‘databaseName‘];
        global $con;
        echo "CREATE DATABASE " . $dbName;
        $q = mysql_query("CREATE DATABASE ".$dbName , $con);
        if($q){
            echo "创建创建成功";
        }else{
            echo "创建失败 " . mysql_errno();
        }
    }
    //创建person表
    function createTable_PERSON(){
        global $con;
        mysql_select_db("xudongyang" , $con);
        $sql = "CREATE TABLE person (
            firstName varchar(15),
            lastName varchar(15),
            age int(5))";
        echo "创建数据局 <br />";
        echo $sql . "<br />";
        mysql_query($sql , $con);
        echo "创建完毕";
    }
    
    mysql_close($con);
?>
<html>
    <head>
        <title></title>
        <meta charset="utf-8"/>
    </head>
    <body>
        <div>
            <fieldset id="">
                <legend>创建数据库</legend>
                <input type="text" /><button id="createDataBaseBtn">创建</button>
            </fieldset>
        </div>
        
        <script src="http://www.mamicode.com/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
        <script type="text/javascript">
            $(function(){
                $(‘#createDataBaseBtn‘).click(function(){
                    var val = $(this).prev().val();
                    location.href = http://www.mamicode.com/‘?databaseName=‘ + val;
                });
            })
        </script>
    </body>
</html>

 

php连接数据库、创建数据库、创建数据表