小插曲,晚上把資料的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);?>
結果:
感謝閱讀,希望能協助到大家,謝謝大家對本站的支援!