A php connection class to the mysql database

Source: Internet
Author: User
Tags mysql host
A php connection class to the mysql database

  1. /*
  2. * Filename: mysql database connection class
  3. * Edit bbs.it-home.org
  4. */
  5. Class mysql {
  6. Private $ db_host; // database host
  7. Private $ db_user; // database username
  8. Private $ db_pwd; // database username and password
  9. Private $ db_database; // database name
  10. Private $ conn; // Database Connection ID;
  11. Private $ result; // result resource ID for executing the query command
  12. Private $ SQL; // SQL execution statement
  13. Private $ row; // number of returned entries
  14. Private $ coding; // database encoding, GBK, UTF8, gb2312
  15. Private $ bulletin = true; // whether to enable error records
  16. Private $ show_error = true; // in the test phase, all errors are displayed, which has security risks and is disabled by default.
  17. Private $ is_error = false; // determines whether an error is terminated immediately. the default value is true. we recommend that you do not enable the error because it is annoying to see nothing when there is a problem.
  18. /* Constructor */
  19. Public function _ construct ($ db_host, $ db_user, $ db_pwd, $ db_database, $ conn, $ coding ){
  20. $ This-> db_host = $ db_host;
  21. $ This-> db_user = $ db_user;
  22. $ This-> db_pwd = $ db_pwd;
  23. $ This-> db_database = $ db_database;
  24. $ This-> conn = $ conn;
  25. $ This-> coding = $ coding;
  26. $ This-> connect ();
  27. }
  28. /* Database connection */
  29. Public function connect ()
  30. {
  31. If ($ this-> conn = "pconn "){
  32. // Permanent link
  33. $ This-> conn = mysql_pconnect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
  34. } Else {
  35. // Instant link
  36. $ This-> conn = mysql_connect ($ this-> db_host, $ this-> db_user, $ this-> db_pwd );
  37. }
  38. If (! Mysql_select_db ($ this-> db_database, $ this-> conn )){
  39. If ($ this-> show_error ){
  40. $ This-> show_error ("database unavailable:", $ this-> db_database );
  41. }
  42. }
  43. Mysql_query ("set names $ this-> coding ");
  44. }
  45. /* Database execution statements: execute queries, add, modify, delete, and other SQL statements */
  46. Public function query ($ SQL)
  47. {
  48. If ($ SQL = ""){
  49. $ This-> show_error ("SQL statement error:", "SQL query statement is empty ");}
  50. $ This-> SQL = $ SQL;
  51. $ Result = mysql_query ($ this-> SQL, $ this-> conn );
  52. If (! $ Result ){
  53. // Used during debugging. the SQL statement is automatically printed when an error occurs.
  54. If ($ this-> show_error ){
  55. $ This-> show_error ("incorrect SQL statement:", $ this-> SQL );
  56. }
  57. } Else {
  58. $ This-> result = $ result;
  59. }
  60. Return $ this-> result;
  61. }
  62. /* Create and add a new database */
  63. Public function create_database ($ database_name ){
  64. $ Database = $ database_name;
  65. $ SqlDatabase = 'create database'. $ database;
  66. $ This-> query ($ sqlDatabase );
  67. }
  68. /* Query all databases on the server */
  69. // Separate the system database from the user database for more intuitive display?
  70. Public function show_databases (){
  71. $ Rs = $ this-> query ("show databases ");
  72. Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
  73. Echo"
    ";
  74. $ I = 1;
  75. While ($ row = $ this-> fetch_array ($ rs )){
  76. Echo "$ I $ row [Database]";
  77. Echo"
    ";
  78. $ I ++;
  79. }
  80. }
  81. // Returns the names of all databases on the host in an array.
  82. Public function databases ()
  83. {
  84. $ RsPtr = mysql_list_dbs ($ this-> conn );
  85. $ I = 0;
  86. $ Cnt = mysql_num_rows ($ rsPtr );
  87. While ($ I <$ cnt)
  88. {
  89. $ Rs [] = mysql_db_name ($ rsPtr, $ I );
  90. $ I ++;
  91. }
  92. Return $ rs;
  93. }
  94. /* Query all tables in the database */
  95. Function show_tables ($ database_name ){
  96. $ This-> query ("show tables ");
  97. Echo "existing database:". $ amount = $ this-> db_num_rows ($ rs );
  98. Echo"
    ";
  99. $ I = 1;
  100. While ($ row = $ this-> fetch_array ($ rs )){
  101. $ ColumnName = "Tables_in _". $ database_name;
  102. Echo "$ I $ row [$ columnName]";
  103. Echo"
    ";
  104. $ I ++;
  105. }
  106. }
  107. /*
  108. Mysql_fetch_row () array $ row [0], $ row [1], $ row [2]
  109. Mysql_fetch_array () array $ row [0] or $ row [id]
  110. Mysql_fetch_assoc () array is case sensitive with the $ row-> content field
  111. Mysql_fetch_object () object is case sensitive with $ row [id] and $ row [content] Fields
  112. */
  113. /* Obtain result data */
  114. Public function mysql_result_li ()
  115. {
  116. Return mysql_result ($ str );
  117. }
  118. /* Obtain the record set and array-index and association. use $ row ['content'] */
  119. Public function fetch_array ()
  120. {
  121. Return mysql_fetch_array ($ this-> result );
  122. }
  123. // Obtain the joined array, using $ row ['Field name']
  124. Public function fetch_assoc ()
  125. {
  126. Return mysql_fetch_assoc ($ this-> result );
  127. }
  128. // Obtain the numeric index array, using $ row [0], $ row [1], $ row [2]
  129. Public function fetch_row ()
  130. {
  131. Return mysql_fetch_row ($ this-> result );
  132. }
  133. // Obtain the object array using $ row-> content
  134. Public function fetch_Object ()
  135. {
  136. Return mysql_fetch_object ($ this-> result );
  137. }
  138. // Simplified query select
  139. Public function findall ($ table)
  140. {
  141. $ This-> query ("SELECT * FROM $ table ");
  142. }
  143. // Simplified query select
  144. Public function select ($ table, $ columnName, $ condition)
  145. {
  146. If ($ columnName = ""){
  147. $ ColumnName = "*";
  148. }
  149. $ This-> query ("SELECT $ columnName FROM $ table $ condition ");
  150. }
  151. // Simplify del deletion
  152. Public function delete ($ table, $ condition ){
  153. $ This-> query ("delete from $ table WHERE $ condition ");
  154. }
  155. // Simplify insert
  156. Public function insert ($ table, $ columnName, $ value ){
  157. $ This-> query ("insert into $ table ($ columnName) VALUES ($ value )");
  158. }
  159. // Simplify update modification
  160. Public function update ($ table, $ mod_content, $ condition ){
  161. $ This-> query ("UPDATE $ table SET $ mod_content WHERE $ condition ");
  162. }
  163. /* Obtain the ID generated by the previous INSERT operation */
  164. Public function insert_id (){
  165. Return mysql_insert_id ();
  166. }
  167. // Point to a specific data record
  168. Public function db_data_seek ($ id ){
  169. If ($ id> 0 ){
  170. $ Id = $ id-1;
  171. }
  172. If (! @ Mysql_data_seek ($ this-> result, $ id )){
  173. $ This-> show_error ("SQL statement error:", "the specified data is empty ");
  174. }
  175. Return $ this-> result;
  176. }
  177. // Calculate the number of result sets based on the select query results
  178. Public function db_num_rows (){
  179. If ($ this-> result = null ){
  180. If ($ this-> show_error ){
  181. $ This-> show_error ("SQL statement error", "temporarily empty, no content! ");
  182. }
  183. } Else {
  184. Return mysql_num_rows ($ this-> result );
  185. }
  186. }
  187. // Obtain the number of affected rows based on insert, update, and delete execution results
  188. Public function db_affected_rows (){
  189. Return mysql_affected_rows ();
  190. }
  191. // Output the SQL statement
  192. Public function show_error ($ message = "", $ SQL = ""){
  193. If (! $ SQL ){
  194. Echo "". $ message ."";
  195. Echo"
    ";
  196. } Else {
  197. Echo" ";
  198. Echo"Error message:
    ";
  199. Echo"

    ";

  200. Echo"

    ";

  201. Echo "error code: 12142 ";
  202. Echo"


    ";
  203. Echo "error cause:". mysql_error ()."

    ";
  204. Echo"

    ";

  205. Echo "". $ message ."";
  206. Echo"

    ";
  207. Echo"
    ".$sql."
    ";
  208. $ Ip = $ this-> getip ();
  209. If ($ this-> bulletin ){
  210. $ Time = date ("Y-m-d H: I: s ");
  211. $ Message = $ message. "/r/n $ this-> SQL ". "/r/n customer IP address: $ ip ". "/r/n time: $ time ". "/r/n ";
  212. $ Server_date = date ("Y-m-d ");
  213. $ Filename = $ server_date. ". txt ";
  214. $ File_path = "error/". $ filename;
  215. $ Error_content = $ message;
  216. // $ Error_content = "wrong database, cannot be connected ";
  217. $ File = "error"; // sets the file storage directory.
  218. // Create a folder
  219. If (! File_exists ($ file )){
  220. If (! Mkdir ($ file, 0777 )){
  221. // The default mode is 0777, which means the maximum possible access.
  222. Die ("upload files directory does not exist and creation failed ");
  223. }
  224. }
  225. // Create a txt date file
  226. If (! File_exists ($ file_path )){
  227. // Echo "create date file ";
  228. Fopen ($ file_path, "w + ");
  229. // First, make sure that the file exists and is writable.
  230. If (is_writable ($ file_path ))
  231. {
  232. // Open $ filename in add mode. the file pointer will start with the file
  233. If (! $ Handle = fopen ($ file_path, 'A '))
  234. {
  235. Echo "the file $ filename cannot be opened ";
  236. Exit;
  237. }
  238. // Write $ somecontent to the open file.
  239. If (! Fwrite ($ handle, $ error_content ))
  240. {
  241. Echo "cannot be written to file $ filename ";
  242. Exit;
  243. }
  244. // Echo "file $ filename written successfully ";
  245. Echo "-- the error record is saved! ";
  246. // Close the file
  247. Fclose ($ handle );
  248. } Else {
  249. Echo "file $ filename cannot be written ";
  250. }
  251. } Else {
  252. // First, make sure that the file exists and is writable.
  253. If (is_writable ($ file_path ))
  254. {
  255. // Open $ filename in add mode. the file pointer will start with the file
  256. If (! $ Handle = fopen ($ file_path, 'A '))
  257. {
  258. Echo "the file $ filename cannot be opened ";
  259. Exit;
  260. }
  261. // Write $ somecontent to the open file.
  262. If (! Fwrite ($ handle, $ error_content ))
  263. {
  264. Echo "cannot be written to file $ filename ";
  265. Exit;
  266. }
  267. // Echo "file $ filename written successfully ";
  268. Echo "-- the error record is saved! ";
  269. // Close the file
  270. Fclose ($ handle );
  271. } Else {
  272. Echo "file $ filename cannot be written ";
  273. }
  274. }
  275. }
  276. Echo"
    ";
  277. If ($ this-> is_error ){
  278. Exit;
  279. }
  280. }
  281. Echo"

    ";
  282. Echo"
  283. ";
  284. Echo"
    ";
  285. }
  286. // Release the result set
  287. Public function free (){
  288. @ Mysql_free_result ($ this-> result );
  289. }
  290. // Database selection
  291. Public function select_db ($ db_database ){
  292. Return mysql_select_db ($ db_database );
  293. }
  294. // Query the number of fields
  295. Public function num_fields ($ table_name ){
  296. // Return mysql_num_fields ($ this-> result );
  297. $ This-> query ("select * from $ table_name ");
  298. Echo"
    ";
  299. Echo "number of fields:". $ total = mysql_num_fields ($ this-> result );
  300. Echo"
    ";
  301. for ($i=0; $i<$total; $i++){
  302. print_r(mysql_fetch_field($this->result,$i) );
  303. }
  304. echo "
  305. ";
  306. Echo"
    ";
  307. }
  308. // Obtain MySQL server information
  309. Public function mysql_server ($ num = ''){
  310. Switch ($ num ){
  311. Case 1:
  312. Return mysql_get_server_info (); // MySQL server information
  313. Break;
  314. Case 2:
  315. Return mysql_get_host_info (); // Obtain MySQL host information
  316. Break;
  317. Case 3:
  318. Return mysql_get_client_info (); // Obtain MySQL client information
  319. Break;
  320. Case 4:
  321. Return mysql_get_proto_info (); // Obtain MySQL protocol information
  322. Break;
  323. Default:
  324. Return mysql_get_client_info (); // Obtain the mysql version by default.
  325. }
  326. }
  327. // Destructor, which automatically closes the database and recycles garbage
  328. Public function _ destruct ()
  329. {
  330. If (! Empty ($ this-> result )){
  331. $ This-> free ();
  332. }
  333. Mysql_close ($ this-> conn );
  334. } // Function _ destruct ();
  335. /* Obtain the real IP address of the client */
  336. Function getip (){
  337. If (getenv ("HTTP_CLIENT_IP") & strcasecmp (getenv ("HTTP_CLIENT_IP"), "unknown "))
  338. {
  339. $ Ip = getenv ("HTTP_CLIENT_IP ");
  340. }
  341. Else if (getenv ("HTTP_X_FORWARDED_FOR") & strcasecmp (getenv ("HTTP_X_FORWARDED_FOR"), "unknown ")){
  342. $ Ip = getenv ("HTTP_X_FORWARDED_FOR ");
  343. }
  344. Else if (getenv ("REMOTE_ADDR") & strcasecmp (getenv ("REMOTE_ADDR"), "unknown "))
  345. {
  346. $ Ip = getenv ("REMOTE_ADDR ");
  347. }
  348. Else if (isset ($ _ SERVER ['remote _ ADDR ']) & $ _ SERVER ['remote _ ADDR '] & strcasecmp ($ _ SERVER ['remote _ ADDR'], "unknown ")){
  349. $ Ip = $ _ SERVER ['remote _ ADDR '];
  350. }
  351. Else {
  352. $ Ip = "unknown ";
  353. }
  354. Return ($ ip );
  355. }
  356. }
  357. ?>

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.