一、修改thinkphp的主頁面
(一)使用sublime打開已安裝完成的thinkphp的tp目錄
使用sublime打開已安裝完成的thinkphp的tp目錄

在thinkphp的tp目錄輸入cmd打開后

輸入php run think,運行thinkphp

打開瀏覽器,輸入http://127.0.0.1:8000/,thinkphp運行成功

(二)修改Index.php
打開tp\app\controller\Index.php,下面是Index.php的內(nèi)容

將以下內(nèi)容
return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14載初心不改 - 你值得信賴的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a target="yisu">億速云</a> 獨家贊助發(fā)布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
修改為
return 'hello,thinkphp';
(三)運行thinkphp,查看主頁面
瀏覽器刷新頁面后,修改成功

(四)在Index.php添加function
打開tp\app\controller\Index.php,輸入
public function hello2($name = 'ThinkPHP6')
{
return 'hello2,' . $name;
}

(五)保存并運行thinkphp
瀏覽器輸入http://127.0.0.1:8000/index/hello2,出現(xiàn)以下頁面則function運行成功

但是,瀏覽器輸入http://127.0.0.1:8000/index/hello,報錯

解決辦法:
打開tp\router\app.php,將下面這一句注釋掉,它影響了hello的運行
Route::get('hello/:name', 'index/hello');

瀏覽器刷新,則運行成功

二、連接數(shù)據(jù)庫
(一)下載安裝mysql
(1)打開官網(wǎng)https://dev.mysql.com/downloads/mysql/,下載mysql壓縮包

下載成功后,將它解壓,解壓成功后,進入mysql-8.0.21-winx64\mysql-8.0.21-winx64\bin,找到mysql.exe進行安裝

(2)創(chuàng)建data文件夾,創(chuàng)建my.ini
在解壓好的mysql-8.0.21-winx64文件夾中,新建一個文件夾,命名為data(注意這里的data文件夾必須是空的)

在mysql-8.0.21-winx64下面,創(chuàng)建一個my.ini文件

將以下代碼復(fù)制進入my.ini中
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.
[mysqld]
#skip-grant-tables
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
basedir ="F:\mysql-8.0.21-winx64" # 設(shè)置mysql的安裝目錄
datadir ="F:\mysql-8.0.21-winx64\data" # 設(shè)置mysql數(shù)據(jù)庫的數(shù)據(jù)的存放目錄,必須是data,或者是//xxx/data
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
#服務(wù)端的編碼方式
character-set-server=utf8mb4
[client]
#客戶端編碼方式,最好和服務(wù)端保存一致
loose-default-character-set=utf8mb4
[WinMySQLadmin]
Server = "F:\mysql-8.0.21-winx64\bin\mysqld.exe"
請注意:這里有三處地方需要修改為你安裝mysql的目錄:
①basedir ="F:\mysql-8.0.21-winx64" # 設(shè)置你的安裝mysql的目錄
②datadir ="F:\mysql-8.0.21-winx64\data" # 設(shè)置你的mysql數(shù)據(jù)庫的data文件夾的存放目錄,必須是data
③Server = "F:\mysql-8.0.21-winx64\bin\mysqld.exe" # 設(shè)置你自己 mysql數(shù)據(jù)庫的mysqld.exe文件的存放目錄

(3)添加環(huán)境變量
此電腦右擊,屬性——高級系統(tǒng)設(shè)置——環(huán)境變量——系統(tǒng)變量——Path
添加:F:\mysql-8.0.21-winx64\bin

(4)以管理員身份打開“命令行窗口”
必須要以管理員身份運行,輸入命令mysqld --initialize-insecure --user=mysql

完成后,可以在mysql-8.0.21-winx64目錄下看到,原本為空的data,運行完之后里面多了許多文件和文件夾

如果沒有以管理員的身份運行cmd,則輸入mysqld -install,就會出現(xiàn)下面的報錯信息

原因就是沒有用管理員的身份打開cmd
解決辦法:
輸入cmd,右擊,以管理員身份運行

再次輸入mysqld -install,就會成功,顯示Service successfully installed.(表示Mysql服務(wù)添加成功)
輸入命令net start mysql,啟動Mysql服務(wù)

(5)打開cmd,登錄mysql后,修改密碼(默認(rèn)密碼為空)
①以管理員身份打開“命令行窗口”,輸入mysql -uroot -p,確認(rèn)
②輸入時,繼續(xù)按下Enter(空密碼),登錄mysql
③輸入命令use mysql;(切換到mysql數(shù)據(jù)庫)
④輸入命令A(yù)LTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密碼'; (設(shè)置密碼)
⑤輸入命令flush privileges; (設(shè)置完密碼后,需要刷新MySQL的系統(tǒng)權(quán)限相關(guān)表,否則會出現(xiàn)拒絕訪問)
⑥輸入命令exit;(退出)

(二)下載安裝navicat
下載navicat
鏈接: https://pan.baidu.com/s/13g4tdtx_c_VjD8CP3xlpnw 提取碼: k1mi
下載完成后找到navicat.exe

點擊注冊,輸入注冊碼key后,即可使用
三、連接數(shù)據(jù)庫
(一)使用navicat連接數(shù)據(jù)庫
雙擊“navicat.exe”,選擇“連接”

連接名:localhost,密碼是剛才修改mysql的密碼

連接完成后,右擊,打開連接

連接成功的效果圖:

(二)新建數(shù)據(jù)庫
對任何一個數(shù)據(jù)庫,選擇右擊,選擇“新建數(shù)據(jù)庫”

數(shù)據(jù)庫名:test
字符集:utf8mb4 -- UTF-8 Unicode
確認(rèn)

(三)添加表
test數(shù)據(jù)庫,右擊,打開數(shù)據(jù)庫,找到表,右擊,新建表

id int 不允許為空 設(shè)為主鍵
name varchar 100 可為空 (注釋:姓名)
保存為user
(id這里一定要√“自動遞增”,否則在后面數(shù)據(jù)的插入過程中會出現(xiàn)錯誤)

(四)添加表數(shù)據(jù)
1 張三
2 李四
輸入完成功之后,ctrl+s保存

(五)修改配置文件
打開tp\config\database.php,修改
/**寫上你的數(shù)據(jù)庫**/
'database' => env('database.database', 'test'),
/**寫上你的數(shù)據(jù)庫的密碼**/
'password' => env('database.password', '12345678'),

打開tp.example.env,另存為.env,將錯誤提示信息打開寫入用戶的賬號和密碼
APP_DEBUG = true
[APP]
DEFAULT_TIMEZONE = Asia/Shanghai
[DATABASE]
TYPE = mysql
HOSTNAME = 127.0.0.1
DATABASE = test
USERNAME = root
PASSWORD = 12345678
HOSTPORT = 3306
CHARSET = utf8
DEBUG = true
[LANG]
default_lang = zh-cn

四、實現(xiàn)增刪改查
(一)查找數(shù)據(jù)
(1)根據(jù)id查找
使用sublime打開tp,再打開tp\app\controller\Index.php
添加下面的代碼
public function index()
{
// 查詢數(shù)據(jù)(根據(jù)id)
$res = Db::table('user')->where('id', 1)->select();
return $res;
// 查詢結(jié)果:[{"id":1,"name":"張三"}]
}

瀏覽器輸入http://127.0.0.1:8000,就會查詢到id=1的信息

(2)根據(jù)name查找
添加下面的代碼
public function index2()
{
// 查詢數(shù)據(jù)(根據(jù)name)
$res = Db::table('user')->where('name', '李四')->select();
return $res;
// 查詢結(jié)果:[{"id":2,"name":"李四"}]
}

瀏覽器輸入http://127.0.0.1:8000/index/index2,就會查詢到name=2的信息

(二)插入數(shù)據(jù)
打開navicat,對user表右擊,設(shè)計表,插入字段
address varchar 255 0
按ctrl+s保存

雙擊user表,對它進行刷新,就可以看到address字段

(1)利用save方法插入
添加下面的代碼
// 插入數(shù)據(jù)(利用save方法)
public function insert($name = 'ThinkPHP6')
{
$data = ['address' => 'bar', 'name' => '王五'];
$res = Db::name('user')->save($data);
return $res;
// 查詢結(jié)果:1
}

瀏覽器輸入http://127.0.0.1:8000/index/insert,就會出現(xiàn)1,表示插入成功

打開user表查看,有王五這一行,表示插入成功

(2)利用save方法插入
添加下面的代碼
// 插入數(shù)據(jù)(利用insertGetId方法)
public function insert2($name = 'ThinkPHP6')
{
$data = ['address' => 'bar', 'name' => '王五'];
$res = Db::name('user')->insertGetId($data);
return $res;
// 查詢結(jié)果:4
}

瀏覽器輸入http://127.0.0.1:8000/index/insert2,就會出現(xiàn)4,表示插入成功

打開user表查看,有王五,id為4這一行,表示插入成功

(三)更新數(shù)據(jù)
(1)利用save方法進行更新
添加下面的代碼
public function update($name = 'ThinkPHP6')
{
$res = Db::name('user')
->save(['id' => 1, 'name' => 'thinkphp']);
return $res;
// 查詢結(jié)果:1
}

瀏覽器輸入http://127.0.0.1:8000/index/update,就會出現(xiàn)1,表示更新成功

打開user表查看,有id為1,name為thinkphp這一行,表示更新成功

(2)利用update方法進行更新
添加下面的代碼
// 更新數(shù)據(jù)(利用update方法)
public function update2($name = 'ThinkPHP6')
{
$res = Db::name('user')
->where('name', 'thinkphp')
->update(['name' => '修改']);
return $res;
// 查詢結(jié)果:1
}

瀏覽器輸入http://127.0.0.1:8000/index/update2,就會出現(xiàn)1,表示更新成功

打開user表查看,有id為1,name為修改這一行,表示更新成功

(四)刪除數(shù)據(jù)
(1)刪除單個數(shù)據(jù)
添加下面的代碼
// 刪除數(shù)據(jù)(刪除單個數(shù)據(jù))
public function delete()
{
$res = Db::table('user')->delete(1);
return $res;
// 查詢結(jié)果:1
}

瀏覽器輸入http://127.0.0.1:8000/index/delete,就會出現(xiàn)1,表示更新成功

打開user表查看,沒有有id為1,name為修改這一行,表示刪除成功

(2)刪除一組數(shù)據(jù)
添加下面的代碼
// 刪除數(shù)據(jù)(刪除一組數(shù)據(jù))
public function delete2()
{
$res = Db::table('user')->delete([2,3]);
return $res;
// 查詢結(jié)果:2
}

瀏覽器輸入http://127.0.0.1:8000/index/delete,就會出現(xiàn)2,表示刪除成功

打開user表查看,沒有有id為2,name為李四,id為3,name為王五這兩行,表示刪除成功

詳情可以參照thinkphp的官方文檔:
https://www.kancloud.cn/manual/thinkphp6_0/1037530
五、出現(xiàn)的錯誤
進行增刪改查時,瀏覽器出現(xiàn)“could not find driver”,原因是php.ini里面沒有開放mysql
解決辦法:
找到php7\php.ini,用sublime打開

將extension=pdo_mysql前面的英文“;”刪除,保存

重啟php,則可以進行增刪改查
