PHP 操作MySQL

來源:互聯網
上載者:User

標籤:成功   mysql資料庫   count   dbn   code   增強   操作   函數   mysql使用者   

 

php中操作mysql資料庫的函數

php最初提供一套串連操作mysql函數 mysql_xxx();
後來提供了增強版函數 mysqli_xxx();

使用php串連mysql伺服器步驟
1:建立到mysql伺服器串連
$conn = mysqli_connect(url,uname,upwd,dbname);
url mysql伺服器位址 ‘127.0.0.1‘
uname mysql使用者名稱 ‘root‘
upwd mysql密碼 ‘‘
dbname 串連哪個庫 ‘dangdang‘
2:向mysql伺服器發送sql語句
$result = mysqli_query(資料連線,sql語句);
3:讀取mysql伺服器返回結果
if($result===true){
執行成功
}else{
執行失敗
}
4:斷開與mysql伺服器串連---(可以省略)
mysqli_close($conn);

樣本:8.php 添加資料
dangdang/dd_category(id,name,bcount);
坑:
php->資料庫記亂碼!
原因:預設情況下 php-->mysql添加資料
編碼latin_1
解決:在建立串連之後加一行指令
$conn = mysqli_conn.......
mysqli_query($conn,"SET NAMES UTF8");
PHP發送sql轉碼--latin_1-->utf8

執行個體代碼:

<?php    $conn=mysqli_connect(‘127.0.0.1‘,‘root‘,‘‘,‘dangdang‘) or die(‘伺服器串連失敗!‘);    mysqli_query($conn,‘set names utf8‘);    $sql=‘select * from dd_book‘;    $re=mysqli_query($conn,$sql);    if($re){        foreach($re as $k=>$v){            var_dump($k,$v);        }    }else{        throw new Exception(‘資料表查詢失敗!‘);    }

 

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.