Connecting to a database
Here is the simplest PHP connection to the MySQL database code:
The code is as follows |
Copy Code |
<?php $link =mysql_connect ("localhost", "root", "password"); if (! $link) echo "Connect error"; else echo "Connect OK"; ?> |
where mysql_connect () connection function, localhost on behalf of the database server address, root is the MySQL database user name, password is the MySQL database password. When you use it, you can change it to your own.
To make it easier to use later, normalize the connection code:
The code is as follows |
Copy Code |
<?php $link _host= ' localhost '; $link _user= ' root '; $link _pass= ' password ';
$link =mysql_connect ($link _host, $link _user, $link _pass);
if ($link) { echo "Connect ok!"; } Else { echo "Connect fail!"; } ?>
|
Use three variables to read the server address, user name and password to facilitate the subsequent form reading and assignment, etc.
Building a Database
#建立数据库代码
The code is as follows |
Copy Code |
<?php Include ("conn.php");
$link _db= ' Link_system '; To set the name of the database you want to build, you must not have the same name as the database you already have
if ($link) { echo "Connect ok!<br/>"; if (mysql_query ("CREATE DATABASE" $link _db, $link)) { echo "Database created!<br/>"; } Else { echo "Database Create fail!"; } } Else { echo "Connect error!"; } ?> |
Once you have established the Link_system database, you need to create a table.
#建立数据库表格
To set up the table to be built is link_table, the following is the name of the table to be established, used to store different data, can be set according to their own needs.
ID of the LINK_ID data
Link_name Friend chain Name
Link_url Friend chain Web site
Link_detail Introduction
Link_contact Contact method
Link_show whether to display
Link_order Arrange Order
Link_sort classification
The code is as follows |
Copy Code |
Because we have friends in the list of categories, so we need to create a classification table link_sorts, my idea is to save a friend chain to show the location, such as the home page or channel pages, pages and so on. sort_id Data ID Sort_name category Name The complete PHP code for creating the table is as follows: <?php Select the database for the operation mysql_select_db ($link _db, $link);
Create a table $link _table = "CREATE TABLE link_table ( link_id int unsigned primary key not NULL auto_increment, Link_name varchar is not NULL, Link_url varchar (m) NOT NULL, Link_detail varchar (m) NOT NULL, Link_contact varchar (m) NOT NULL, link_show int unsigned NOT NULL, link_order int unsigned NOT NULL, link_sort int unsigned NOT NULL )";
$sort _table = "CREATE TABLE sort_table ( sort_id int unsigned primary key not NULL auto_increment, Sort_name varchar () NOT NULL )";
Performing a table-building operation if (!mysql_query ($link _table, $link)) { echo "Create link_table error:". Mysql_error (). "<br/>"; } else { echo "Link_table created!". "<br/>"; }
if (!mysql_query ($sort _table, $link)) { echo "Create sort_table error:". Mysql_error (). "<br/>"; } else { echo "Sort_table created!". "<br/>"; }
Finish closing the database connection Mysql_close ($link); ?> |
If the execution succeeds, the database is completed! The next step is to add the data.
Add data
First, create a table to fill in data that needs to be written to the MySQL database:
The code is as follows |
Copy Code |
#写入数据库 insert.php <form action= "insert_ok.php" method= "POST" > Website name: <input type= "text" name= "Site_Name"/> <br/> Site Links: <input type= "text" value= "http://" name= "Site_url"/> <br/> Introduction: <input type= "text" value= "no" name= "Site_detail"/> <br/> Contact: <input type= "text" name= "Site_contact"/> <br/> Sort: <input type= "Text" value= "1" name= "Site_order"/> <br/> Category: <input type= "Text" value= "1" name= "Site_sort"/> <br/> Show: <input name= "site_show" type= "checkbox" id= "checkbox" value= "1" checked= "checked"/> <br/> <input type= "Submit"/> </form> |
All the rest are entered using a text box, and whether the check box is displayed or not, is selected by default.
The code is as follows |
Copy Code |
Executing a written program page insert_ok.php <?php Include ("conn.php");
Read the data from the form in the previous page $link _name=$_post[site_name]; $link _url=$_post[site_url]; $link _contact=$_post[site_contact]; $link _detail=$_post[site_detail]; $link _order=$_post[site_order]; $link _sort=$_post[site_sort]; $link _show=$_post[site_show];
if (! $link _show== "1") $link _show= "0"; check box is selected, if not checked, assign a value of 0
mysql_select_db ("Link_system", $link); Select Database Link_system
if ($_post) { $sql = "INSERT into link_table (link_name,link_url,link_contact,link_detail,link_order,link_sort,link_show) VALUES (' $link _name ', ' $link _url ', ' $link _contact ', ' $link _detail ', ' $link _order ', ' $link _sort ', ' $link _show '); if (!mysql_query ($sql, $link)) { echo "Failed to add data:". Mysql_error (); } Else { echo "Add Data Success!" "; Echo $_post[site_name]. " <br> ". $_post[site_url]." <br> ". $_post[site_contact]." <br> ". $_post[site_detail]." <br> ". $_post[site_order]." <br> ". $_post[site_sort]." <br> ". $_post[site_show]; } } ?> |
If the execution succeeds, then adds the friend chain data completes, as to the classification temporarily does not add, until later will classify joins inside. The next step is to display the data, edit the data, and delete the implementation of the data.
Show write database data
To plan what we want to display, first of all, because it is a friend chain display page, you need to show the title of the friend chain, chain links, friend chain of the description, and installation we add friend chain when the custom order to arrange.
Links Show Page
view.php
The code is as follows |
Copy Code |
<?php include (" Conn.php "); mysql_select_db ("Link_system", $link);//Select Database $exec = "SELECT * FROM Link_table Link_order "; //sql The query statement by sorting the custom fields mysql_query ("SET link_name GB2312"); $result = mysql_query ($ exec, $link); Gets the dataset if (! $result) {die ("Valid result!"); Echo <p> display effect:</p>; while ($rs = Mysql_fetch_array ($result)) { $name = $rs [' link_name ']; $url = $rs [' Link_url ']; $detail = $rs [' Link_detail ']; $show = $rs [' link_show ']; if ($detail = = "None") $alt = $name Else $alt = $detail; if ($show = = "1") echo "<a href = "$url" title= $alt target=_blank> $name </a><br> "; } Mysql_free_result ($result);//close DataSet |
This is the link to show the friendship, the late in accordance with their own needs of the effect, readjust.