Home > php教程 > php手册 > body text

PHP入门篇:读写MySQL数据库(读写 删除 添加)

WBOY
Release: 2016-05-23 13:07:24
Original
2038 people have browsed it

读写MySQL是需要利用mysql_connect来与mysql数据库连接,连接数据库之后我们就可以使用mysql中的sql语句来读写数据库了,下面来整一下例子.

连接数据库

下面是最简单的PHP连接MySQL数据库的代码:

<?php  
$link=mysql_connect("localhost","root","password");  
if (!$link) echo "connect error";  
else echo "connect ok phprm";
Copy after login


其中mysql_connect()连接函数,localhost代表数据库服务器的地址,root是MySql数据库用户名,password是MySql数据库的密码,使用时改成自己的即可.

为了更方便以后使用,将连接代码规范化一下:

<?php  
$link_host=&#39;localhost&#39;;  
$link_user=&#39;root&#39;;  
$link_pass=&#39;password&#39;;  
$link=mysql_connect($link_host,$link_user,$link_pass);  
if ($link)  
{  
    echo "connect ok!";  
}  
else 
{  
    echo "connect fail!";  
}
Copy after login

用三个变量来读取服务器地址、用户名和密码,方便以后进行表单读取和赋值等.

建立数据库代码如下:

<?php 
include ("conn.php"); 
$link_db=&#39;link_system&#39;; 
//设置要建立的数据库的名字,一定不能跟已有的数据库名称相同 
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!"; 
}
Copy after login

建立了link_system数据库之后,还需要建立表格.

#建立数据库表格 link_table,下面是需要建立的表名,用来存储不同的数据,可以根据自己的需要来设置.

link_id 数据的id

link_name 友链名称

link_url 友链网址

link_detail 简介

link_contact 联系方式

link_show 是否显示

link_order 排列顺序

link_sort 分类

因为我们友链表中有分类,所以需要建立一个分类表link_sorts,我的设想是存友链显示的位置,比如首页或者频道页、内页等.

sort_id 数据id

sort_name 分类名称

建立表格的完整PHP代码如下:

<?php 
//选择操作的数据库 
mysql_select_db($link_db,$link); 
//建立表格 
$link_table = "create table link_table 
( 
link_id int unsigned primary key not null auto_increment, 
link_name varchar(20) not null, 
link_url varchar(50) not null, 
link_detail varchar(100) not null, 
link_contact varchar(100) 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(20) not null 
)"; 
//执行建表操作 
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 />"; 
} 
//执行完毕关闭数据库连接 
mysql_close($link);
Copy after login

如果执行成功,则建立数据库完毕,开始下一步就是添加数据了.

添加数据,首先建立一个表格,用来填写需要向MySQL数据库写入的数据,代码如下:

#写入数据库

//insert.php

<form action="insert_ok.php" method="post"> 
网站名称: <input type="text" name="site_name" /> 
<br /> 
网站链接: <input type="text" value="http://" name="site_url" /> 
<br /> 
简介: <input type="text" value="无" name="site_detail" /> 
<br /> 
联系方式: <input type="text" name="site_contact" /> 
<br /> 
排序: <input type="text" value="1" name="site_order" /> 
<br /> 
分类: <input type="text" value="1" name="site_sort" /> 
<br /> 
是否显示: <input name="site_show" type="checkbox" id="checkbox" value="1" checked="checked"  /> 
<br /> 
<input type="submit" /> 
</form>
Copy after login


这里其他的都是用文本框输入,而是否显示使用复选框来实现,默认选中,代码如下:

//执行写入的程序页面

//insert_ok.php

<?php 
include ("conn.php"); 
//读取上个页面中表单中的数据 
$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"; 
//复选框是否选中,如果没有选中则赋值为0 
mysql_select_db("link_system", $link); //选择数据库link_system 
if($_POST) 
{ 
    $sql = "INSERT INTO link_table (link_name,link_url,link_contact,link_detail,link_order,link_sort,link_show) VALUES (&#39;$link_name&#39;,&#39;$link_url&#39;,&#39;$link_contact&#39;,&#39;$link_detail&#39;,&#39;$link_order&#39;,&#39;$link_sort&#39;,&#39;$link_show&#39;)"; 
    if(!mysql_query($sql,$link)) 
    { 
        echo "添加数据失败:".mysql_error(); 
    } 
    else 
    { 
        echo "添加数据成功!"; 
 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]; 
    } 
}
Copy after login

如果执行成功,则添加友链数据完成,至于分类暂时先不添加,到后期再将分类加入里面,下一步则是显示数据、编辑数据和删除数据的实现了.

显示写入数据库数据

规划一下我们要显示的内容吧,首先因为是友链展示页面,则需要显示友链的标题、友链的锚链接、友链的描述,并且安装我们添加友链时候自定义的顺序来排列.

//友情链接展示页面

view.php

<?php 
include ("conn.php"); 
mysql_select_db("link_system", $link); //选择数据库 
$exec = "SELECT * FROM link_table order by link_order"; 
//SQL查询语句,按照自定义字段的排序来查询 
mysql_query("SET link_name GB2312"); 
$result = mysql_query($exec, $link); //获取数据集 
if(!$result){die("Valid result!");} 
echo "<p>显示效果:</p>"; 
while($rs = mysql_fetch_array($result)) 
{ 
    $name=$rs[&#39;link_name&#39;]; 
    $url=$rs[&#39;link_url&#39;]; 
    $detail=$rs[&#39;link_detail&#39;]; 
    $show=$rs[&#39;link_show&#39;]; 
    if ($detail == "无") $alt=$name; 
    else $alt=$detail; 
    if ($show == "1") echo "<a href="$url" title=$alt target=_blank>$name</a><br>"; 
} 
mysql_free_result($result); //关闭数据集
Copy after login

这样就把友情链接显示出来了,后期根据自己需要的效果,重新调整.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!