修改thinkphp的主頁面,連接數(shù)據(jù)庫,實現(xiàn)增刪改查

一、修改thinkphp的主頁面

(一)使用sublime打開已安裝完成的thinkphp的tp目錄

使用sublime打開已安裝完成的thinkphp的tp目錄


tp.png

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

打開cmd.png

輸入php run think,運行thinkphp

運行thinkphp.png

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

thinkphp運行成功.png

(二)修改Index.php

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

Index.php.png

將以下內(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,查看主頁面

瀏覽器刷新頁面后,修改成功


修改成功.png

(四)在Index.php添加function

打開tp\app\controller\Index.php,輸入

    public function hello2($name = 'ThinkPHP6')
    {
        return 'hello2,' . $name;
    }
function存放位置.png

(五)保存并運行thinkphp

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

運行thinkphp成功.png

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

報錯.png

解決辦法:
打開tp\router\app.php,將下面這一句注釋掉,它影響了hello的運行

Route::get('hello/:name', 'index/hello');
注釋.png

瀏覽器刷新,則運行成功


運行成功.png

二、連接數(shù)據(jù)庫

(一)下載安裝mysql

(1)打開官網(wǎng)https://dev.mysql.com/downloads/mysql/,下載mysql壓縮包
mysql.png

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

mysql.exe.png
(2)創(chuàng)建data文件夾,創(chuàng)建my.ini

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

新建空的data文件夾.png

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

my.ini.png

將以下代碼復(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文件的存放目錄

修改配置文件,更改為你安裝mysql的目錄.png

(3)添加環(huán)境變量

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


添加環(huán)境變量.png
(4)以管理員身份打開“命令行窗口”

必須要以管理員身份運行,輸入命令mysqld --initialize-insecure --user=mysql

運行命令.png

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

data.png

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

Install/Remove of the Service Denied!.png

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

cmd.png

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

添加與啟動mysql.png
(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;(退出)

修改mysql密碼.png

(二)下載安裝navicat

下載navicat
鏈接: https://pan.baidu.com/s/13g4tdtx_c_VjD8CP3xlpnw 提取碼: k1mi

下載完成后找到navicat.exe

navicat.exe.png

點擊注冊,輸入注冊碼key后,即可使用

三、連接數(shù)據(jù)庫

(一)使用navicat連接數(shù)據(jù)庫

雙擊“navicat.exe”,選擇“連接”

連接.png

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

連接信息.png

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

打開連接.png

連接成功的效果圖:

連接成功.png

(二)新建數(shù)據(jù)庫

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

新建數(shù)據(jù)庫.png

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

新建數(shù)據(jù)庫信息.png

(三)添加表

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

新建表.png

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

表的配置信息.png

(四)添加表數(shù)據(jù)

1 張三
2 李四
輸入完成功之后,ctrl+s保存

表數(shù)據(jù).png

(五)修改配置文件

打開tp\config\database.php,修改

/**寫上你的數(shù)據(jù)庫**/
'database'          => env('database.database', 'test'),
/**寫上你的數(shù)據(jù)庫的密碼**/
'password'          => env('database.password', '12345678'),
修改database.php.png

打開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
.env的信息.png

四、實現(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":"張三"}]
    }
根據(jù)id查找.png

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

查詢到id=1的信息.png
(2)根據(jù)name查找

添加下面的代碼

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

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

查詢到name=2的信息.png

(二)插入數(shù)據(jù)

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


添加address字段.png

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

address字段添加成功.png
(1)利用save方法插入

添加下面的代碼

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

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

插入成功.png

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

插入成功的數(shù)據(jù).png
(2)利用save方法插入

添加下面的代碼

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

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

插入成功.png

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

插入成功的數(shù)據(jù).png

(三)更新數(shù)據(jù)

(1)利用save方法進行更新

添加下面的代碼

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

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

利用save更新數(shù)據(jù)成功.png

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

利用save更新數(shù)據(jù)成功.png
(2)利用update方法進行更新

添加下面的代碼

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

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

利用update更新數(shù)據(jù)成功.png

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

利用update更新數(shù)據(jù)成功.png

(四)刪除數(shù)據(jù)

(1)刪除單個數(shù)據(jù)

添加下面的代碼

    // 刪除數(shù)據(jù)(刪除單個數(shù)據(jù))
    public function delete()
    {
        $res = Db::table('user')->delete(1);
        return $res;
        // 查詢結(jié)果:1
    }
刪除單個數(shù)據(jù).png

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

單個數(shù)據(jù)刪除成功.png

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

單個數(shù)據(jù)刪除成功.png
(2)刪除一組數(shù)據(jù)

添加下面的代碼

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

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

刪除一組數(shù)據(jù)成功.png

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

刪除一組數(shù)據(jù)成功.png

詳情可以參照thinkphp的官方文檔:
https://www.kancloud.cn/manual/thinkphp6_0/1037530

五、出現(xiàn)的錯誤

進行增刪改查時,瀏覽器出現(xiàn)“could not find driver”,原因是php.ini里面沒有開放mysql
解決辦法:
找到php7\php.ini,用sublime打開

php.ini所在位置.png

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

打開mysql權(quán)限.png

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

成功結(jié)果舉例.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容