php筆記08:資料庫編程---使用php的MySQL擴充庫操作MySQL資料庫

來源:互聯網
上載者:User

標籤:

1.使用php的MySQL擴充庫操作MySQL資料庫:

php有3種方式操作MySQL資料庫 (1)mysql擴充庫 (2)mysqli擴充庫 (3)pdo    mysql擴充庫與mysql資料庫區別 ?
php設計者封裝了一些方法去操作mysql資料庫,這些方法集中起來形成了mysql擴充庫。 mysql資料庫存放資料。     2.使用php的 MySQL擴充庫操作MySQL資料庫案例: (1)環境搭建:啟用MySQL資料庫,在php.ini檔案中配置使用MySQL資料庫,           extension = php_mysql.dll 可以通過:在http://localhost/test.php,這裡的test.php文件之中寫到: <?php     phpinfo() ;//輸出php可以使用的擴充庫,驗證是否開啟了mysql服務 ?>   (2 )建立一張使用者表,供我們使用: create table user1(        id int  primary  key  auto_increment ,        name varchar(32) not null ,        passwd  varchar(64) not null ,        email   varchar(128) not null ,           age   tinyint   unsigned  not null ) ;   預先加入資料: insert into user1(name,passwd, email,age) values (‘zs‘, md5(‘123456‘), ‘[email protected]‘ , 30) ; insert into user1(name,passwd, email,age) values (‘ls‘, md5(‘123456‘), ‘[email protected]‘ , 40) ;
insert into user1(name,passwd, email,age) values (‘ww‘, md5(‘123456‘), ‘[email protected]‘ , 50) ;
insert into user1(name,passwd, email,age) values (‘評委‘, md5(‘123456‘), ‘[email protected]‘ , 50) ;
  這裡插入中文會報錯:需要進行下面設定: show variables like ‘%char%‘; set character_set_client = gbk; set character_set_client = gbk; (2)編寫php程式,完成對使用者表的顯示: 步驟:-->1: 擷取串連:擷取mysql擴充庫和mysql資料庫的串連-->2: 選擇資料庫-->3: 設定編碼(建議有)-->4: 發送指令sql-->5: 接收返回結果,並處理。(顯示)-->6: 釋放資源,關閉串連
<?php    header("Content-Type: text/html; charset=utf-8");   //mysql擴充庫操作mysql資料庫步驟如下:   //1.擷取串連:擷取mysql擴充庫和mysql資料庫的串連   $conn = mysql_connect("127.0.0.1","root","root");//參數1:主機名稱,參數2:使用者名稱,參數3:密碼;   if(!$conn) {       die("串連失敗".mysql_error());   }   //2.選擇資料庫   mysql_select_db("test");      //3.設定編碼(建議有)   //mysql_    //4.發送指令sql(ddl 資料定義語句),dml (資料動作陳述式 update insert delete),dql(資料查詢語言 select) ,dtl (資料事務語言 rollback commit...)   $sql = "select *from user1";   //函數   //$res表示結果集,你可以簡單的理解就是一張表   $res = mysql_query($sql,$conn);   //var_dump($res);   //5.接收返回結果,並處理。(顯示)   //mysql_fetch_row會依次取出$res結果集的下一行資料,賦值給$row   //$row就是一個數組   while($row = mysql_fetch_row($res)){       //第一種:echo "<br/> $row[0]--$row[1]--$row[2]";       //第二種:echo "<br/>";       //        var_dump($row);       //第三種:       foreach($row as $key=> $val) {           echo "--$val";       }       echo "<br/>";   }   //6.釋放資源,關閉串連     mysql_free_result($res);   mysql_close($conn);      ?>

 上面代碼提到了三種顯示資料的方法結果如下:

    第一種:echo "<br/> $row[0]--$row[1]--$row[2]";

 

    第二種:echo "<br/>"; var_dump($row);

  第三種:foreach($row as $key=> $val) {                           echo "--$val";                 }                echo "<br/>";  

 

 

mysql_free_result($res); //釋放佔用的電腦資源,否則電腦會越來越卡!

 mysql_close($conn);//釋放串連,如果沒有這一句,我們反覆重新整理頁面訪問.../mysql/mysqlDemo1.php,會出現很多TIME_WAIT的串連請求:如下

反覆重新整理頁面之後,我們輸入一下cmd命令:


查看結果:

 



php筆記08:資料庫編程---使用php的MySQL擴充庫操作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.