php串連MySQL資料庫及增刪改查

來源:互聯網
上載者:User

標籤:適合   編碼   database   ble   狀態   sign   ram   prepare   bsp   

  1、串連MySQL資料庫$conn = new mysqli($host,$user,$password);

$conn -> query(‘set names utf8‘);      //設定字元編碼,避免存入中文資料亂碼
$conn -> select_db(‘db‘);         
 //選擇自己建立的資料庫(db)

1.1 判斷串連狀態
if (!$conn) {
  die("
資料庫連接異常");
}

//資料庫的建立
$db = "create database db";
if($conn -> query($db)){
echo "成功建立資料庫";
}else{
echo "建立失敗";
}

//資料表的建立
$table = "create table table_name(
id int(10) unsigned auto_increment unique primary key,
user varchar(100) NOT NULL
);";

//1.1.1 添加資料方法1:預先處理語句及綁定參數方法(適合多條查詢語句的執行)
$table = $conn -> prepare("insert into user_table(id,user) values(NULL,?)");//準備sql語句
$name = ‘小明‘;      
$table -> bind_param("s",$name); //(類型:s,類型值);參數s為string,i為interger,d為double,類型值僅可為一變數
if ($table -> execute() && $conn -> affected_rows){ //判斷是否添加成功
   echo "添加成功"; 
}else{
   echo "添加失敗";
}
//1.1.1 添加資料方法二:直接調用query()方法(適合單條語句;多條用";"隔開,用multi_query()方法查詢);
$insert = "insert into table(name) values(‘你好‘)";
if ($conn -> query($insert) && $conn -> affected_rows){
  echo "成功添加";
}else{
  echo "添加失敗";  
}

//1.1.2 刪除資料方法1直接調用query()方法;
$delete = "delete from user_table where user in(‘張三‘,‘1‘) or id in(1)";//刪除user=‘張三‘的列名 或 id=1的列名
if($conn -> query($delete) && $row = $conn -> affected_rows){
echo "刪除成功,一共:$row"."行";
}else{
  ehco "刪除失敗";
}
//1.1.2 刪除資料方法2:預先處理語句及綁定參數
$delete = $conn -> prepare("delete from user_table where id in(?) or user in(?);");
$id = 1;
$name = ‘張三‘;
$delete -> bind_param(‘ii‘,$id,$name);
if($delete -> execute() && $row = $conn -> affected_rows){
echo "刪除成功,一共:$row"."行";
}else{
  echo "刪除失敗";
}

//1.1.3 更改資料方法1:直接query()方法
$update = "update user_table set user = ‘劉老師‘ where id = ‘115‘;";
if($conn -> query($update) && $row = $conn -> affected_rows){
  echo "更改:$row"."行";
}else{
  echo "更改失敗";
}

//1.1.3 更改資料方法2:預先處理語句及綁定參數
$update = $conn -> prepare("update user_table set user = ? where id = ?;");
$user = "黃老師";
$id = 1;
$update -> bind_param("si",$user,$id);
if($update -> execute() && $row = $conn -> affected_rows){
echo "更改:$row"."行";
}else{
  echo "更改失敗";
}
//1.1.4 查詢資料方法1:直接query()方法
$select = "select id,user from user_table ORDER BY user,id DESC "; $result = $conn -> query($select);
if($result -> num_rows>0){              // ORDER BY排序 DESC降序排序
while ($row = $result -> fetch_array()){    //迴圈查詢並返回指定條件的每條語句
echo "$row[id]:$row[user]<br>";
}
}else{
  echo "沒有資料";
}
1.2 面向過程方法
$conn = mysqli_connect($this ->host,$this -> user,$this -> password);
mysqli_query($conn, ‘set names utf8‘);      //設定字元編碼,避免存入中文資料亂碼
mysqli_select_db($conn, ‘db‘);          //選擇自己建立的資料庫(db)

1.2.1增刪改查(同理上面);
即:mysqli_query() == $conn -> query();

1.3 PDO方法(略)


 

php串連MySQL資料庫及增刪改查

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.