PHP MySQL database links and operations

Source: Internet
Author: User

During development, I often deal with databases and record the methods for establishing links between PHP and MySQL.

1. <? PHP 2. class MySQL {3. private $ host = "localhost"; // database address 4. private $ user = "root"; // database account 5. private $ pass = "lizhihua"; // Database Password 6. private $ dbname = "db_iqoomea"; // database name 7. private $ conn; 8. /** 9. * constructor: connect to the Database 10. * @ return true: Connection successful; false: Connection Failed. 11. */12. function MySQL () {13. $ user = $ this-> User; 14. $ pass = $ this-> pass; 15. $ host = $ this-> host; 16. $ db = $ this-> dbname; 17. // connect to the database 18. $ conn = mysql_connect ($ host, $ user, $ pass); 19. mysql_select_db ($ db, $ conn); 20. mysql_query ("set names utf8"); 21. $ this-> conn = $ conn; 22. return true; 23 .} 24. /** 25. * Database Table query 26. * For example, select * from Table 27. */28. function getallinfo ($ table) {29. If (empty ($ table) return false; 30. if (empty ($ this-> conn) return false; 31. $ conn = $ this-> conn; 32. // send an SQL statement and obtain result 33. $ SQL = "select * from $ table"; 34. $ result = mysql_query ($ SQL, $ conn); 35. if ((! $ Result) or (empty ($ result) {36. return false; 37 .} 38. $ num = 0; 39. $ DATA = array (); 40. // put the query result in a two-dimensional array 41. while ($ ROW = mysql_fetch_array ($ result) {42. $ data [$ num] = $ row; 43. $ num ++; 44 .} 45. mysql_free_result ($ result); 46. return $ data; 47 .} 48. 49. // It can be used to insert, modify, update, and delete data tables. 50 data tables are created. function query ($ strsql) {51. if (empty ($ strsql) return false; 52. if (empty ($ this-> conn) Return False; 53. $ conn = $ this-> conn; 54. $ result = mysql_query ($ strsql, $ conn); 55. If (! Result) return false; 56. return $ result; 57 .} 58. 59. function select ($ table, $ field, $ findname) {60. if (empty ($ table) return false; 61. if (empty ($ field) return false; 62. if (empty ($ findname) return false; 63. if (empty ($ this-> conn) return false; 64. $ conn = $ this-> conn; 65. $ SQL = "select * from $ table where $ field = '$ findname'"; 66. $ result = mysql_query ($ SQL, $ conn); 67. if ((! $ Result) or (empty ($ result) {68. return false; 69 .} 70. $ DATA = mysql_fetch_array ($ result); 71. mysql_free_result ($ result); 72. return $ data; 73 .} 74. 75. // query a single record 76 is returned Based on the query conditions. function getinfo ($ strsql) {77. if (empty ($ strsql) return false; 78. if (empty ($ this-> conn) return false; 79. $ conn = $ this-> conn; 80. // send an SQL statement and obtain result 81. $ result = mysql_query ($ strsql, $ conn); 82. if ((! $ Result) or (empty ($ result) {83. return false; 84 .} 85. $ DATA = mysql_fetch_array ($ result); 86. mysql_free_result ($ result); 87. return $ data; 88 .} 89. 90. // multiple records are returned Based on the query conditions. 91. function getmoreinfo ($ strsql) {92. if (empty ($ strsql) return false; 93. if (empty ($ this-> conn) return false; 94. $ conn = $ this-> conn; 95. $ result = mysql_query ($ strsql, $ conn); 96. if ((! $ Result) or (empty ($ result) {97. return false; 98 .} 99. $ num = 0; 100. $ DATA = array (); 101. // put the query result in a two-dimensional array of 102. while ($ ROW = mysql_fetch_array ($ result) {103. $ data [$ num] = $ row; 104. $ num ++; 105 .} 106. mysql_free_result ($ result); 107. return $ data; 108 .} 109. // Based on the query conditions, 110 is returned for multiple records. // function getallinfo ($ table) {111. // If (empty ($ table) return false; 112. // If (empty ($ this-> conn)) Return false; 113. // $ conn = $ this-> conn; 114. // $ SQL = "select * from $ table"; 115. // $ result = mysql_query ($ SQL, $ conn); 116. // If ((! $ Result) or (empty ($ result) {117. // return false; 118. //} 119. // $ num = 0; 120. // $ DATA = array (); 121. //// put the query result in a two-dimensional array of 122. // while ($ ROW = mysql_fetch_array ($ result) {123. // $ data [$ num] = $ row; 124. // $ num ++; 125. //} 126. // mysql_free_result ($ result); 127. // return $ data; 128. //} 129. 130. // The number of rows affected (insert, query, update, and delete) is 131. function getrowsnum ($ strsql) {132. if (empty ($ strsql)) Return false; 133. if (empty ($ this-> conn) return false; 134. $ conn = $ this-> conn; 135. $ result = mysql_query ($ strsql, $ conn); 136. $ num = mysql_num_rows ($ result); 137. mysql_free_result ($ result); 138. return $ num; 139 .} 140. 141. function getallrowsnum ($ table) {142. if (empty ($ table) return false; 143. if (empty ($ this-> conn) return false; 144. $ conn = $ this-> conn; 145. $ SQL = "select * From $ table "; 146. $ result = mysql_query ($ SQL, $ conn); 147. $ num = mysql_num_rows ($ result); 148. mysql_free_result ($ result); 149. return $ num; 150 .} 151. 152. /* It can be used to modify data 153. * $ tablename table name 154. * $ setporpertyname: The field name modified is 155. * $ setvalue: the modified value is 156. * $ id indicates ID 157.*158. */159. function Update ($ strsql) {160. if (empty ($ strsql) return false; 161. if (empty ($ this-> conn) return false; 162. $ Conn = $ this-> conn; 163. // send an SQL statement and update database 164. $ result = mysql_query ($ strsql, $ conn); 165. If (! $ Result) return false; 166. return $ result; 167 .} 168. 169. // Delete the record 170 for an ID of the specified table. function Delete ($ tablename, $ id) {171. if (empty ($ tablename) return false; 172. if (empty ($ this-> conn) return false; 173. $ conn = $ this-> conn; 174. $ strsql = "delete from $ tablename where id = '$ id'"; 175. $ result = mysql_query ($ strsql, $ conn); 176. return true; 177 .} 178. 179. // returns an array containing the table. Therefore, the array contains 180 fields. function getf Ields ($ table) {181. if (empty ($ table) return false; 182. if (empty ($ this-> conn) return false; 183. $ conn = $ this-> conn; 184. $ SQL = "show full fields from $ table"; 185. $ showresult = mysql_query ($ SQL, $ conn); 186. $ showdata = array (); 187. while ($ showrow = mysql_fetch_array ($ showresult) {188. $ showdata [] = $ showrow ['field']; 189 .} 190. mysql_free_result ($ showresult); 191. return $ showdata; 192 .} 193. // return the number of fields in a table, which is 194. function getfieldsnum ($ table) {195. if (empty ($ table) return false; 196. if (empty ($ this-> conn) return false; 197. $ conn = $ this-> conn; 198. $ SQL = "select * from $ table"; 199. $ showresult = mysql_query ($ SQL, $ conn); 200. $ getcount = mysql_num_fields ($ showresult); 201. mysql_free_result ($ showresult); 202. return $ getcount; 203 .} 204. 205 .} 206.?>

Example

  1.    1. <?php   2. header("content-Type: text/html; charset=utf-8");   3. session_start();   4. include "../include/conn.inc";   5. $str=microtime();   6. $arr=explode(" ",$str);   7. $start = $arr[0] * 1000;   8. $consql = new Mysql();   9. mysql_query('SET NAMES utf8;');  10. $valuetype = 2;  11.  12. $musicname = $_GET['musicname'];  13.  14. //$sqlsec = "select * from SecondDay_Info where CityName='$musicname'";  15.  16. $Song_Info = array();  17. $Singer_Info = array();  18. $Album_Info = array();  19. $SecondDay_Info = array();  20. $strSQL = "select * from liricstable where singer_name='$musicname'";  21. if($valuetype == 2){  22.     $Song_Info = $consql->getmoreinfo($strSQL);  23.     print_r($str1);  24.     foreach($Song_Info as $rows){  25.         $str = $rows['song_name'] . "----" . $rows['singer_name'] . "----" . $rows['album_name'] . "----" . $rows['liric_path'] . "----" . $rows[album_cover_path] . "\n";  26.         print_r($str);  27.     }  28. }  29. ?>

The Code logic is simple. You can see the meaning of the comment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.