php mysql連接數(shù)據(jù)庫實(shí)例
小插曲,晚上把數(shù)據(jù)的my.ini編碼改為utf-8,然后數(shù)據(jù)庫一直不能啟動(dòng),改回gbk就可以,有知道的告知下問題所在。
因?yàn)槭擎溄訑?shù)據(jù)庫,也沒什么好說明的,直接上代碼吧。
<?php /* Connect to a MySQL server 連接數(shù)據(jù)庫服務(wù)器 */ $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 連接數(shù)據(jù)庫名稱*/ if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); exit; }else echo '數(shù)據(jù)庫連接上了!'; /* Close the connection 關(guān)閉連接*/ mysqli_close($link); ?>
這里的jian其實(shí)是其中一個(gè)名字為jian的庫的管理員,不需要寫數(shù)據(jù)庫管理員,因?yàn)閷?shí)際項(xiàng)目我們可能得到的就是一個(gè)庫管理員的數(shù)據(jù)庫賬號、密碼、還有主機(jī)地址。
測試結(jié)果:
原來的數(shù)據(jù)庫表:
操作1:查詢分?jǐn)?shù)大于60的學(xué)生id,班級和分?jǐn)?shù)
SELECT id,class,scores FROM jian_scores WHERE scores>60
全部代碼:
<?php /* Connect to a MySQL server 連接數(shù)據(jù)庫服務(wù)器 */ $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 連接數(shù)據(jù)庫名稱*/ if (!$link) { printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error()); exit; }else echo '數(shù)據(jù)庫連接上了!'. "<br/>"; if ($result = mysqli_query($link, 'SELECT id,class,scores FROM jian_scores WHERE scores>60 ')) { echo('id 班級 分?jǐn)?shù) '). "<br/>"; /* Fetch the results of the query 返回查詢的結(jié)果 */ 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 結(jié)束查詢釋放內(nèi)存 */ mysqli_free_result($result); } /* Close the connection 關(guān)閉連接*/ mysqli_close($link); ?>
結(jié)果:
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
mysql read_buffer_size 設(shè)置多少合適
很多朋友都會(huì)問mysql read_buffer_size 設(shè)置多少合適,其實(shí)這個(gè)都是根據(jù)自己的內(nèi)存大小等來設(shè)置的2016-05-05mysql5.7.14 解壓版安裝配置方法圖文教程(win10)
這篇文章主要為大家詳細(xì)介紹了win10下mysql5.7.14 winx64安裝配置方法圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12mysql實(shí)現(xiàn)if語句判斷功能的6種使用形式小結(jié)
這篇文章主要給大家介紹了關(guān)于mysql實(shí)現(xiàn)if語句判斷功能的6種使用形式,MySQL的IF既可以作為表達(dá)式用,也可在存儲(chǔ)過程中作為流程控制語句使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07Mysql實(shí)驗(yàn)之使用explain分析索引的走向
索引是mysql的必須要掌握的技能,同時(shí)也是提供mysql查詢效率的手段。通過以下的一個(gè)實(shí)驗(yàn)可以理解?mysql的索引規(guī)則,同時(shí)也可以不斷的來優(yōu)化sql語句2018-01-01Starting MySQL.Manager of pid-file quit without updating fil
因?yàn)橛脖P滿了,Starting MySQL.Manager of pid-file quit without updating file.[FAILED]2010-02-02mysql執(zhí)行時(shí)間為負(fù)數(shù)的原因分析
今天看到有人把phpmyadmin中的執(zhí)行時(shí)間出現(xiàn)負(fù)數(shù)的情況視為phpmyadmin的bug, 其實(shí)這種情況的本質(zhì)是php中浮點(diǎn)數(shù)(float)的精度問題。2010-08-08