PHP5 and MySQL database operations common code collection _php basics

Source: Internet
Author: User
1 Establish database tables:
Copy Code code as follows:

Create DATABASE Club;
CREATE TABLE member (
ID Int (one) not NULL auto_increment,
No varchar (5) NOT NULL,
Name varchar (ten) is not NULL,
Age Int (2) is not NULL,
Level varchar (ten) is 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 ', ' HJ ', 1, ' 2008-04-02 00:00:00 '),
(2, ' C022 ', ' Liyan ', Don, ' Zs ', 1, ' 2007-05-31 00:00:00 '),
(3, ' A006 ', ' Zhangyan ', +, ' HJ ', 1, ' 2007-06-20 00:00:00 '),
(4, ' B052 ', ' luanying ', "the", "BJ", 1, ' 2007-02-12 00:00:00 '),
(5, ' A007 ', ' Duxiang ', num, ' HJ ', 2, ' 2008-03-26 00:00:00 '),
(6, ' C060 ', ' Liuyu ', ' Zs ', 1, ' 2008-10-16 00:00:00 ');


2 reading Data
2.1 Establish 01.php
Code
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title> Members List </title>
<?php
$link =mysql_connect ("localhost", "root", "123"); Connecting to the MySQL server
$db =mysql_select_db ("club"); Select Database
mysql_query ("Set names UTF8", $link); Set encoding mode
$sql = "SELECT * from member";
$result =mysql_query ($sql, $link); Execute SELECT query
$num =mysql_num_rows ($result); Get a record query
?>
<body>
<br/>
Click on the name to view the details of the member, 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> Sex </td>
</tr>
<?php
while ($row =mysql_fetch_array ($result))
{
echo "<tr><td>". $row [' id ']. " </td><td><a href=member.php?name= "
. $row [' name ']. " > ". $row [' name ']." </a></td><td> "
. ($row [' Sex ']==1?] Female ":" Male ")." </td></tr> ";
}
?>
</table>
<?php
}
Else
{
The Echo Club has not yet developed its membership. ";
}
?>
</body>

2.2 Establishment of Member.php
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title> member Details </title>
<?php
$link =mysql_connect ("localhost", "root", "123"); Connecting to the MySQL server
$db =mysql_select_db ("club"); Select Database
mysql_query ("Set names UTF8", $link); Set encoding mode
$sql = "Select No,name,sex,age,level,date_format (date, '%y-%c-%d ') as Join_date from
." where Name= ' ". Trim ($_get[' name '))." ' ";
$result =mysql_query ($sql, $link); Execute in SELECT query
?>
<body>
<?php
if ($row =mysql_fetch_array ($result))
{
echo "Number:". $row [' No ']. " <br/> ";
echo "Name:". $row [' name ']. " <br/> ";
echo "Sex:". ($row [' Sex ']==1?] Female ":" Male ")." <br/> ";
echo "Age:". $row [' ages ']. " <br/> ";
echo "Level:". $row [' Levels ']. " <br/> ";
echo "Add:" $row [' join_date ']. " <br/> ";
}
?>
</body>


3 Modifying Data
3.1 Establish level.php (modify data)
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title> Club Promotions </title>
<body>
<?php
$link =mysql_connect ("localhost", "root", "123"); Connecting to the MySQL server
$db =mysql_select_db ("club"); Select Database
mysql_query ("Set name UTF8", $link); Set encoding mode
$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 "Rank: Platinum Member Number:". $row [' num ']. " <br/> ";
Break
Case ' HJ ':
echo "Rank: Number of Gold Members:". $row [' num ']. " <br/> ";
Break
Default
echo "Rank: Number of Diamond Members:". $row [' num ']. " <br/> ";
}
}
?>
<form action= "up_level.php" name= "level" method= "POST" >
Membership Promotion: From
<select name= "Old_level" >
<option value= "HJ" > Gold Member </option>
<option value= "BJ" > Platinum Member </option>
</select>
Upgrade to
<select name= "New_level" >
<option value= "BJ" > Platinum Member </option>
<option value= "ZS" > Diamond member </option>
</select>
<input type= "Submit" value= "OK"/>
</form>
</body>

3.2 Establishment of up_level.php
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title> Club Promotions </title>
<body>
<?php
$link =mysql_connect ("localhost", "root", "123"); Connecting to the MySQL server
$db =mysql_select_db ("club"); Select Database
mysql_query ("Set name UTF8", $link); Set encoding mode
$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). " Person from ";
Switch (Trim ($_post[' old_level '])) {
Case ' BJ ':
echo "Platinum Member";
Break
Case ' HJ ':
echo "Gold Member";
Break
Default
echo "Diamond Member";
}
echo "successfully upgraded to";
Switch (Trim ($_post[' new_level '])) {
Case ' BJ ':
echo "Platinum Member";
Break
Case ' HJ ':
echo "Gold Member";
Break
Default
echo "Diamond Member";
}
?>
</body>



4 Adding data
4.1 Establishment of add_member.php

Copy Code code as follows:
<meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/>
<title> new member </title>
<body>
<f ORM action= "newmember.php" method= "post" name= "Add_member" >
No.: <input type= "text" name= "no" width= "/>" <br/>
Name: <input type= "text" name= "name" width= ""/><br/>
Sex:
<input type= "Radio" Name= "Sex" value= "1"/> female
<input type= "Radio" name= "Sex" value= "2"/> male <br/>
Age: <input type= "Text" name= "Age" width= "/><br"/>
Levels:
<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& Gt

4.2 Establishment of newmember.php
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title> Add members </title>
<body>
<?php
$link =mysql_connect ("localhost", "root", "123"); Connecting to the MySQL server
$db =mysql_select_db ("club"); Select Database
mysql_query ("Set names GB2312", $link); Set encoding mode
$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); Get the ID of the newly inserted member record
if (Trim ($_post[' level ')) = = "HJ")//Judge new member discount
{
$sql = "Update member set level= ' BJ ' where id= '". $m _id. "'";
$result =mysql_query ($sql, $link); Perform member upgrade offers
$text = "Premium has been upgraded to Platinum membership. ";
}
$sql = "Select *,date_format (date, '%y-%c-%d ') as Join_date from
." where id= ' ". $m _id." ' ";
$result =mysql_query ($sql, $link); Execute SELECT query
if ($row =mysql_fetch_array ($result))
{
echo "New member information: <br/>";
echo "Number:". $row [' No ']. " <br/> ";
echo "Name:". $row [' name ']. " <br/> ";
echo "Sex:". ($row [' Sex ']==1?] Female ":" Male "." <br/> ");
echo "Age:". $row [' ages ']. " <br/> ";
echo "Level:". $row [' Levels ']. " <br/> ";
echo "Add:" $row [' join_date ']. " <br/> ";
}
echo "new member". $row [' name ']. " Add success ". $text;
?>
</body>



5 Creating a class database connection
5.1 Establish cls_mysql.php class files

Copy Code code 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:";
Print_r ($this->error_message);
}
Exit
}
}
?>

5.2 Establishment of test.php
Copy Code code as follows:

<?php
Include ("cls_mysql.php");
?>
<meta http-equiv= "Content-type" content= "text/html;charset=gb2312"/>
<title>mysql Class Library Test </title>
<body>
<?php
$sql = "SELECT * from member";
$db =new cls_mysql (' localhost ', ' root ', ' 123 ', ' Club ', ' GB2312 ');
$result = $db->selectlimit ($sql, ' 3 '); Return 3 member data from the database
if ($result)
{
while ($row = $db->fetch_array ($result))
{
echo "member Number:". $row [' No ']. ", Name:". $row [' name ']. " <br/> ";
}
}
?>
</body>


6 Summary
6.1 mysql_connect (): Establishing a connection to the MySQL server
6.2 mysql_select_db (): Selecting a Database
6.3 mysql_query (): Performing database queries
6.4 mysql_fetch_array (): Getting Database records
6.5 mysql_num_rows (): Get the number of records from a query
6.6 mysql_affected_rows (): Number of rows affected by the most recent operation
6.7 mysql_insert_id (): ID value of last inserted record

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.