Common Code collection for PhP5 and MySQL database operations

Source: Internet
Author: User

1. Create a database table: CopyCode The Code is as follows: Create Database Club;
Create Table member (
Id int (11) not null auto_increment,
No varchar (5) not null,
Name varchar (10) Not null,
Age int (2) not null,
Level varchar (10) Not null,
Sex tinyint (1) not null,
Date datetime not null,
Primary Key (ID)
) Engine = MyISAM default charset = gb2312;
Insert into member (ID, no, name, age, level, sex, date) Values
(1, 'a001', 'wanxia ', 30, 'Hj', 1, '2017-04-02 00:00:00 '),
(2, 'c022', 'liyan ', 29, 'zs', 1, '2017-05-31 00:00:00 '),
(3, 'a006 ', 'zhangyan', 36, 'Hj ', 1, '2017-06-20 00:00:00 '),
(4, 'b052 ', 'luanying', 42, 'bj ', 1, '2017-02-12 00:00:00 '),
(5, 'a007 ', 'duxiang', 26, 'Hj ', 2, '2017-03-26 00:00:00 '),
(6, 'c060', 'liuyu ', 38, 'zs', 1, '2017-10-16 00:00:00 ');


2. Read data
2.1 create 01.php
Code Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> member list </title>
</Head>
<? PHP
$ Link = mysql_connect ("localhost", "root", "123"); // connect to the MySQL server
$ Db = mysql_select_db ("club"); // select a database
Mysql_query ("set names utf8", $ link); // sets the encoding method.
$ SQL = "select * from member ";
$ Result = mysql_query ($ SQL, $ link); // execute SELECT query
$ Num = mysql_num_rows ($ result); // retrieve record Query
?>
<Body>
<H1> fitness club member roster <Br/>
Click the name to view the details of the member. The existing Member <? PHP echo $ num?> Person.
<Br/>
<? PHP
If ($ num> 0)
{
?>
<Table border = "1" cellpadding = "1" cellspacing = "1">
<Tr>
<TD> serial number </TD>
<TD> name </TD>
<TD> gender </TD>
</Tr>
<? PHP
While ($ ROW = mysql_fetch_array ($ result ))
{
Echo "<tr> <TD>". $ row ['id']. "</TD> <a href = member. php? Name ="
. $ Row ['name']. ">". $ row ['name']. "</a> </TD> <TD>"
. ($ Row ['sex'] = 1? "Female": "male"). "</TD> </tr> ";
}
?>
</Table>
<? PHP
}
Else
{
Echo "the club has not yet developed members. ";
}
?>
</Body>
</Html>

2.2 create member. php Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Member details </title>
</Head>
<? PHP
$ Link = mysql_connect ("localhost", "root", "123"); // connect to the MySQL server
$ Db = mysql_select_db ("club"); // select a database
Mysql_query ("set names utf8", $ link); // sets the encoding method.
$ SQL = "Select No, name, sex, age, level, date_format (date, '% Y-% C-% D') as join_date from member"
. "Where name = '". Trim ($ _ Get ['name']). "'";
$ Result = mysql_query ($ SQL, $ link); // execute the query in select
?>
<Body>
<H1> fitness club member details <? PHP
If ($ ROW = mysql_fetch_array ($ result ))
{
Echo "No.:". $ row ['no']. "<br/> ";
Echo "name:". $ row ['name']. "<br/> ";
Echo "Gender:". ($ row ['sex'] = 1? "Female": "male"). "<br/> ";
Echo "Age:". $ row ['age']. "<br/> ";
Echo "level:". $ row ['level']. "<br/> ";
Echo "add:". $ row ['join _ date']. "<br/> ";
}
?>
</Body>
</Html>


3. modify data
3.1 create level. php (modify data) Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> club promotions </title>
</Head>
<Body>
<H1> club member statistical table <? PHP
$ Link = mysql_connect ("localhost", "root", "123"); // connect to the MySQL server
$ Db = mysql_select_db ("club"); // select a database
Mysql_query ("Set Name utf8", $ link); // sets the encoding method.
$ SQL = "select level, count (*) as num from member group by Level ";
$ Result = mysql_query ($ SQL, $ link); // execute SELECT query
While ($ ROW = mysql_fetch_array ($ result ))
{
Switch ($ row ['level']) {
Case 'bj ':
Echo "grade: Platinum Member count:". $ row ['num']. "<br/> ";
Break;
Case 'Hj ':
Echo "grade: Gold member count:". $ row ['num']. "<br/> ";
Break;
Default:
Echo "grade: diamond member count:". $ row ['num']. "<br/> ";
}
}
?>
<Form action = "up_level.php" name = "level" method = "Post">
Member discount upgrade: From
<Select name = "old_level">
<Option value = "HJ"> gold member </option>
<Option value = "BJ"> platinum member </option>
</SELECT>
Upgrade
<Select name = "new_level">
<Option value = "BJ"> platinum member </option>
<Option value = "Zs"> diamond member </option>
</SELECT>
<Input type = "Submit" value = "OK"/>
</Form>
</Body>
</Html>

3.2 Create up_level.php Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> club promotions </title>
</Head>
<Body>
<? PHP
$ Link = mysql_connect ("localhost", "root", "123"); // connect to the MySQL server
$ Db = mysql_select_db ("club"); // select a database
Mysql_query ("Set Name utf8", $ link); // sets the encoding method.
$ SQL = "update Member set level = '". Trim ($ _ post ['new _ level'])
. "'Where level = '". Trim ($ _ post ['old _ level']). "'";
$ Result = mysql_query ($ SQL, $ link); // execute SELECT query
Echo mysql_affected_rows ($ link). "from ";
Switch (TRIM ($ _ post ['old _ level']) {
Case 'bj ':
Echo "platinum member ";
Break;
Case 'Hj ':
Echo "golden member ";
Break;
Default:
Echo "diamond member ";
}
Echo "successfully upgraded ";
Switch (TRIM ($ _ post ['new _ level']) {
Case 'bj ':
Echo "platinum member ";
Break;
Case 'Hj ':
Echo "golden member ";
Break;
Default:
Echo "diamond member ";
}
?>
</Body>
</Html>


4. Add data
4.1 create add_member.php

Copy code The Code is as follows: <HTML>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> new member </title>
<Body>
<H1> new member <Form action = "newmember. php" method = "Post" name = "add_member">
No.: <input type = "text" name = "no" width = "40"/> <br/>
Name: <input type = "text" name = "name" width = "40"/> <br/>
Gender:
<Input type = "radio" name = "sex" value = "1"/> female
<Input type = "radio" name = "sex" value = "2"/> male <br/>
Age: <input type = "text" name = "Age" width = "40"/> <br/>
Level:
<Select name = "level">
<Option value = "HJ"> gold member </option>
<Option value = "BJ"> platinum member </option>
<Option value = "Zs"> diamond member </option>
</SELECT> <br/>
<Input type = "Submit" value = "OK"/>
</Form>
</Body>
</Html>

4.2 create newmember. php Copy code The Code is as follows: <HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"/>
<Title> Add a member </title>
</Head>
<Body>
<? PHP
$ Link = mysql_connect ("localhost", "root", "123"); // connect to the MySQL server
$ Db = mysql_select_db ("club"); // select a database
Mysql_query ("set names gb2312", $ link); // sets the encoding method.
$ SQL = "insert member (No, name, sex, age, level, date) values ('"
. Trim ($ _ post ['no']). "','". Trim ($ _ post ['name']). "','"
. Trim ($ _ post ['sex']). "','". Trim ($ _ post ['age']). "','"
. Trim ($ _ post ['level']). "', now ())";
$ Result = mysql_query ($ SQL, $ link); // execute SELECT query
$ M_id = mysql_insert_id ($ link); // obtain the ID of the newly inserted member record.
If (TRIM ($ _ post ['level']) = "HJ") // determine the new member discount
{
$ SQL = "update Member set level = 'bj 'where id = '". $ m_id ."'";
$ Result = mysql_query ($ SQL, $ link); // executes the membership upgrade offer
$ Text = "you have been given a special offer to upgrade to platinum membership. ";
}
$ SQL = "select *, date_format (date, '% Y-% C-% D') as join_date from member"
. "Where id = '". $ m_id ."'";
$ Result = mysql_query ($ SQL, $ link); // execute SELECT query
If ($ ROW = mysql_fetch_array ($ result ))
{
Echo "new member information: <br/> ";
Echo "No.:". $ row ['no']. "<br/> ";
Echo "name:". $ row ['name']. "<br/> ";
Echo "Gender:". ($ row ['sex'] = 1? "Female": "male". "<br/> ");
Echo "Age:". $ row ['age']. "<br/> ";
Echo "level:". $ row ['level']. "<br/> ";
Echo "add:". $ row ['join _ date']. "<br/> ";
}
Echo "new member". $ row ['name']. "added successfully". $ text;
?>
</Body>
</Html>


5. Create a database-like connection
5.1 create a cls_mysql.php File

Copy code The Code is as follows: <? PHP
Class cls_mysql
{
Protected $ link_id;
Function _ construct ($ dbhost, $ dbuser, $ dbpw, $ dbname = '', $ charset = 'gb2312 ')
{
If (! ($ This-> link_id = mysql_connect ($ dbhost, $ dbuser, $ dbpw )))
{
$ This-> errormsg ("can't pconnect MySQL Server ($ dbhost )! ");
}
Mysql_query ("set names". $ charset, $ this-> link_id );
If ($ dbname)
{
If (mysql_select_db ($ dbname, $ this-> link_id) === false)
{
$ This-> errormsg ("can't slect MySQL database ($ dbname )! ");
Return false;
}
Else
{
Return true;
}
}
}
Public Function select_database ($ dbname)
{
Return mysql_select_db ($ dbname, $ this-> link_id );
}
Public Function fetch_array ($ query, $ result_type = mysql_assoc)
{
Return mysql_fetch_array ($ query, $ result_type );
}
Public Function query ($ SQL)
{
Return mysql_query ($ SQL, $ this-> link_id );
}
Public Function affected_rows ()
{
Return mysql_affected_rows ($ this-> link_id );
}
Public Function num_rows ($ query)
{
Return mysql_num_rows ($ query );
}
Public Function insert_id ()
{
Return_insert_id ($ this-> link_id );
}
Public Function selectlimit ($ SQL, $ num, $ start = 0)
{
If ($ start = 0)
{
$ SQL. = 'limit'. $ num;
}
Else
{
$ SQL. = 'limit'. $ start. ','. $ num;
}
Return $ this-> query ($ SQL );
}
Public Function getone ($ SQL, $ limited = false)
{
If ($ limited = true)
{
$ SQL = trim ($ SQL. 'limit 1 ');
}
$ Res = $ this-> query ($ SQL );
If ($ res! = False)
{
$ ROW = mysql_fetch_row ($ res );
Return $ row [0];
}
Else
{
Return false;
}
}
Public Function getall ($ SQL)
{
$ Res = $ this-> query ($ SQL );
If ($ res! = False)
{
$ Arr = array ();
While ($ ROW = mysql_fetch_assoc ($ res ))
{
$ Arr [] = $ row;
}
Return $ arr;
}
Else
{
Return false;
}
}
Function errormsg ($ message = '', $ SQL = '')
{
If ($ message)
{
Echo "<B> error info </B>: $ message \ n ";
}
Else
{
Echo "<B> MySQL Server Error Report :";
Print_r ($ this-> error_message );
}
Exit;
}
}
?>

5.2 create test. php copy the Code the code is as follows: include ("cls_mysql.php");
?>



MySQL class library test


$ SQL = "select * from member";
$ db = new cls_mysql ('localhost', 'root', '123', 'Club ', 'gb2312');
$ result = $ db-> selectlimit ($ SQL, '3 '); // returns three member information from the database.
if ($ result)
{< br> while ($ ROW = $ db-> fetch_array ($ result ))
{< br> echo "Member No :". $ row ['no']. ", name :". $ row ['name']. "
";
}< BR >?>


6 conclusion
6.1 mysql_connect (): establish a connection with the MySQL server
6.2 mysql_select_db (): select database
6.3 mysql_query (): Execute database query
6.4 mysql_fetch_array (): Obtain database records
6.5 mysql_num_rows (): obtain the number of queried records
6.6 mysql_affected_rows (): number of rows affected by the last operation
6.7 mysql_insert_id (): ID value of the last inserted record

Related Article

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.