next up previous contents
Next: 刪除資料庫 Up: Database 資料庫 Previous: 產生資料庫   Contents

選取工作資料庫

  1. 連線到 mysql,必須選擇工作資料庫,否則每行指令都必須指明要處理的資料庫,比較麻煩,以文字命令列登入工作後,使用 use 命令選取要工作的資料庫為 dyw。
    [root@kvm8 html]# mysql -uroot -p
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 21
    Server version: 5.5.38 MySQL Community Server (GPL) by Remi
    
    Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use dyw;
    Database changed
    mysql> show tables;
    Empty set (0.00 sec)
    
    mysql> exit
    Bye
    
  2. 使用 php 函數 mysql_select 選取資料庫 dywphp。
    [root@kvm8 ~]# cd /var/www/html
    [root@kvm8 html]# vim selectdb.php
    [root@kvm8 html]# cat selectdb.php
    <html>
    <head>
    <title>Selecting MySQL Database</title>
    </head>
    <body>
    <?php
    $dbhost = 'localhost:3036';
    $dbuser = 'dywang';
    $dbpass = '123qwe';
    $conn = mysql_connect($dbhost, $dbuser, $dbpass);
    if(! $conn )
    {
      die('Could not connect: ' . mysql_error());
    }
    echo 'Connected successfully';
    mysql_select_db( 'dywphp' );
    mysql_close($conn);
    ?>
    </body>
    </html>
    
  3. 以 php 命令執行 selectdb.php 選取 dywphp 資料庫,使用瀏覽器開啟 selectdb.php 也可以。
    [root@kvm8 html]# php selectdb.php
    <html>
    <head>
    <title>Selecting MySQL Database</title>
    </head>
    <body>
    Connected successfully</body>
    </html>
    



2017-06-01