PHP學習筆記 1 資料庫的基本操作

2021-08-10 02:38:36 字數 4514 閱讀 5986

1、連線資料庫

<?php

$servername = "localhost";

$username = "root";

$password = "root";

//建立連線

$conn = new mysqli($servername, $username, $password);

//檢測連線

if ($conn->connect_error)

echo

"連線成功"

?>

2、建立資料庫

<?php

$servername = "localhost";

$username = "root";

$password = "root";

//連線資料庫

$conn = new mysqli($servername, $username, $password);

if ($conn->error)

//建立資料庫

$sql = "create database mydb";

if (mysqli_query($conn, $sql)) else

//關閉資料庫

mysqli_close($conn);

?>

3、建立資料表

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

//建立資料庫表

$sql = "create table mygesture (

id int(6) unsigned auto_increment primary key ,

firstname varchar (30) not null ,

lastname varchar (30) not null ,

email varchar (30) not null ,

reg_date timestamp

)";if ($conn->query($sql) == true) else

$conn->close();

?>

4、插入單條資料到資料庫

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

//插入資料

$sql = "insert into mygesture (firstname, lastname, email)

values ('yuan', 'can', '[email protected]')";

if ($conn->query($sql) == ture) else

//關閉資料庫

$conn->close();

?>

5、插入多條資料

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

//插入資料

$sql = "insert into mygesture (firstname, lastname, email)

values ('yuan1', 'can1', '[email protected]');";

$sql .= "insert into mygesture (firstname, lastname, email)

values ('yuan2', 'can2', '[email protected]');";

$sql .= "insert into mygesture (firstname, lastname, email)

values ('yuan3', 'can3', '[email protected]');";

$sql .= "insert into mygesture (firstname, lastname, email)

values ('yuan4', 'can4', '[email protected]')";

if ($conn->multi_query($sql) == ture) else

//關閉資料庫

$conn->close();

?>

6、從資料庫讀取資料

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

$sql = "select id, firstname ,lastname from mygesture";

$result = $conn->query($sql);

if ($result->num_rows > 0)

} else

//關閉資料庫

$conn->close();

?>

7、查詢資料

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

$result = mysqli_query($conn,"select * from mygesture where firstname = 'yuan'");

while ($row = mysqli_fetch_array($result))

//關閉資料庫

$conn->close();

?>

8、修改資料

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

mysqli_query($conn, "update mygesture set firstname = 'huoshan'where lastname = 'can' ");

//關閉資料庫

$conn->close();

?>

9、刪除資料

<?php

$servername = "localhost";

$username = "root";

$password = "root";

$dbname = "mydb";

//建立連線

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error)

mysqli_query($conn, "delete from mygesture where lastname = 'can' ");

//關閉資料庫

$conn->close();

?>

資料庫 mysql學習筆記1之建立等基本操作

1.查詢 select from admin where admin id 1 2.建立資料庫 create database test 3.建立資料表 create table pet name varchar 20 not null default comment 寵物姓名 owner varc...

學習筆記 資料庫基本

必須字母開頭,不要用漢字 必須在1 30個字元之間 必須只能包含a z,a z,0 9,和 不能重名 create table student stuid int not null,stuname varchar 20 not null,char 2 not null,sid int not nul...

Python操作Access資料庫基本操作步驟分析

我們在這篇文章中公分了五個步驟詳細分析了python操作access資料庫的相關方法,希望可以給又需要的朋友們帶來一些幫助。ad python程式語言的出現,帶給開發人員非常大的好處。我們可以利用這樣一款功能強大的物件導向開源語言來輕鬆的實現許多特定功能需求。比如python操作access資料庫的...