PHP連線MYSQL操作增刪改查的原生寫法

2021-09-19 10:00:28 字數 2228 閱讀 7224

配置 config.php

配置mysql連線引數後,引入檔案此就可以操作mysql了

$mysql = [

'host' => '127.0.0.1:3306',

'db' => 'testdb', // 資料庫

'db_user' => 'root', // 使用者名稱

'db_pwd' => 'root', // 密碼

];mysql_connect($mysql['host'], $mysql['db_user'], $mysql['db_pwd']);

mysql_select_db($mysql['db']);

mysql_query('set names utf8');

查詢

引入配置檔案,查詢資料庫的內容

include('./config.php');

$sql = 'select * from cinema';

$res = mysql_query($sql);

$data = array();

while ( $line = mysql_fetch_assoc($res) )

print_r($data);

新增

inset into 表名 (欄位1,欄位2,…) values (內容1,內容2,…)

$sql  = "insert into `cinema` (`movie`, `description`, `rating`) values ('ice song', 'fantacy', '8.6')";

if( mysql_query($sql) )

更新

update 表名 set 欄位1 = 內容1 , 欄位2 = 內容2 where(條件) id = n

更新 id 等於 4的內容

$sql = "update `cinema` set `movie`='singer', `rating`='8.8' where (`id`='4')";

$status = mysql_query($sql);

刪除

delete from 表名 條件 id = n

刪除 id 等於 4 的資料

$sql = "delete from `cinema` where (`id`='4')";

$status = mysql_query($sql);

執行成功返回狀態1

mysql函式詳解:

mysql_fetch_array() 和 mysql_fetch_assoc() 和mysql_fetch_row() 函式之間的區別 ?

示例資料:

insert into `testdb`.`cinema` (`id`, `movie`, `description`, `rating`) values ('1', 'war', 'great 3d', '8.9');
mysql_fetch_array()

函式從結果集中取得一行作為關聯陣列,或數字陣列,或二者兼有

array(

[0] => 1

[id] => 1

[1] => war

[movie] => war

[2] => great 3d

[description] => great 3d

[3] => 8.9

[rating] => 8.9

)

mysql_fetch_assoc

函式從結果集中取得一行作為關聯陣列

array(

[id] => 1

[movie] => war

[description] => great 3d

[rating] => 8.9

)

mysql_fetch_row()

函式從結果集中取得一行作為數字陣列。

array(

[0] => 1

[1] => war

[2] => great 3d

[3] => 8.9

)

Mysql表連線 增刪改操作

1,概要說明 mysql表連線 用在當兩張表或者多張表聯合查詢,共同提供資料 select e.id,d.id,d.name from emp as e join dep as d on e.dpid d.id 2,執行過程 如果是 a join b則a為主表,b為從表 主表中的任何一條資料,都要試...

PHP操作MySQL資料庫(連線 增刪改操作)

mysql 是跟 php 配套使用的最流行的開源資料庫系統,我們知道mysql是php的最佳搭檔,下面是系統的總結php與mysql聯合使用的方法。主要是使用mysql擴充套件,下面就通過歸納總結來提公升。mysql 是一種在 web 上使用的資料庫系統。mysql 是一種在伺服器上執行的資料庫系統...

php連線mysql資料庫的增刪改查操作

自己做的 連線資料庫 url username,password con mysql connect ip.haso soft.com 12038 bscs hasodev hasodev1239 處理結果是亂碼問題 mysql query set character set connection ...