PHP學習筆記:用mysqli鏈接數據庫

小插曲,晚上把數據的my.ini編碼改成utf-8,而後數據庫一直不能啓動,改回gbk就能夠,有知道的告知下問題所在。php

由於是連接數據庫,也沒什麼好說明的,直接上代碼吧。mysql

<?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的庫的管理員,不須要寫數據庫管理員,由於實際項目咱們可能獲得的就是一個庫管理員的數據庫帳號、密碼、還有主機地址。sql

測試結果:數據庫

 

原來的數據庫表:服務器

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

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

所有代碼:fetch

<?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'], "&nbsp;", $row['class'], "&nbsp;",  $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); ?>

結果:編碼

相關文章
相關標籤/搜索