php mysql串連資料庫執行個體_Mysql

來源:互聯網
上載者:User

小插曲,晚上把資料的my.ini編碼改為utf-8,然後資料庫一直不能啟動,改回gbk就可以,有知道的告知下問題所在。

因為是連結資料庫,也沒什麼好說明的,直接上代碼吧。

<?php/* Connect to a MySQL server 串連資料庫伺服器 */$link = mysqli_connect(  'localhost', /* The host to connect to 串連MySQL地址 */  'jian',   /* The user to connect as 串連MySQL使用者名稱 */  '123456', /* The password to use 串連MySQL密碼 */  'jian');  /* The default database to query 串連資料庫名稱*/if (!$link) {  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());  exit;}else  echo '資料庫連接上了!';/* Close the connection 關閉串連*/mysqli_close($link);?>

這裡的jian其實是其中一個名字為jian的庫的管理員,不需要寫資料庫管理員,因為實際項目我們可能得到的就是一個庫管理員的資料庫帳號、密碼、還有主機地址。

測試結果:

 原來的資料庫表:

操作1:查詢分數大於60的學生id,班級和分數

SELECT id,class,scores  FROM jian_scores WHERE scores>60

全部代碼:

<?php/* Connect to a MySQL server 串連資料庫伺服器 */$link = mysqli_connect(  'localhost', /* The host to connect to 串連MySQL地址 */  'jian',   /* The user to connect as 串連MySQL使用者名稱 */  '123456', /* The password to use 串連MySQL密碼 */  'jian');  /* The default database to query 串連資料庫名稱*/if (!$link) {  printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());  exit;}else  echo '資料庫連接上了!'. "<br/>";if ($result = mysqli_query($link, 'SELECT id,class,scores FROM jian_scores WHERE scores>60 ')) {echo('id 班級 分數 '). "<br/>";/* Fetch the results of the query 返回查詢的結果 */while( $row = mysqli_fetch_assoc($result) ){  echo $row['id'], " ", $row['class'], " ", $row['scores'], "<br/>";  // printf("%s (%s) ", $row['id'],$row['class'], $row['scores']);}/* Destroy the result set and free the memory used for it 結束查詢釋放記憶體 */mysqli_free_result($result);}/* Close the connection 關閉串連*/mysqli_close($link);?>

結果:

感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.