对于熟悉做网站的人来说,要想网站做成动态的,肯定要有数据库的支持,利用特定的脚本连接到数据库,从数据库中提取资料、向数据库中添加资料、删除资料等。这里我通过一个实例来说明如何用php连接到数据库的。
我准备建立一个简单的通讯录,数据库的名字叫txl,数据库只有一个表叫personal_info,表中有5个字段
pi_id pi_name pi_tel pi_qq pi_email
首先我们要创建数据库:
create database txl;
然后我们建立表
CREATE TABLE `personal_info` (
`pi_id` bigint(20) NOT NULL auto_increment,
`pi_name` varchar(50) NOT NULL,
`pi_tel` varchar(15) default NULL,
`pi_qq` varchar(15) default NULL,
`pi_email` varchar(50) default NULL,
PRIMARY KEY (`pi_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
上面的sql语句很简单,通过字面都能猜出什么意思。
下面是连接到数据库并且显示表personal_info的所有字段信息:
// connsql.php
<span style="color: #000000;">php </span><span style="color: #800080;">$mysql_server_name</span>="localhost"; <span style="color: #008000;">//</span><span style="color: #008000;">数据库服务器名称</span> <span style="color: #800080;">$mysql_username</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;"> 连接数据库用户名</span> <span style="color: #800080;">$mysql_password</span>="root"; <span style="color: #008000;">//</span><span style="color: #008000;"> 连接数据库密码</span> <span style="color: #800080;">$mysql_database</span>="lxr"; <span style="color: #008000;">//</span><span style="color: #008000;"> 数据库的名字 // 连接到数据库</span> <span style="color: #800080;">$conn</span>=<span style="color: #008080;">mysql_connect</span>(<span style="color: #800080;">$mysql_server_name</span>, <span style="color: #800080;">$mysql_username</span>, <span style="color: #800080;">$mysql_password</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 从表中提取信息的sql语句</span> <span style="color: #800080;">$strsql</span>="select * from personal_info"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 执行sql查询</span> <span style="color: #800080;">$result</span>=<span style="color: #008080;">mysql_db_query</span>(<span style="color: #800080;">$mysql_database</span>, <span style="color: #800080;">$strsql</span>, <span style="color: #800080;">$conn</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取查询结果</span> <span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> '<font face="verdana">'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '<table border="1" cellpadding="1" cellspacing="2">'<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 显示字段名称</span> <span style="color: #0000ff;">echo</span> "/n<tr>/n"<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>mysql_num_fields(<span style="color: #800080;">$result</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;">) { </span><span style="color: #0000ff;">echo</span> '<td bgcolor="#000F00"><b>'. <span style="color: #008080;">mysql_field_name</span>(<span style="color: #800080;">$result</span>, <span style="color: #800080;">$i</span><span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> "</b></td>/n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "</tr>/n"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定位到第一条记录</span> <span style="color: #008080;">mysql_data_seek</span>(<span style="color: #800080;">$result</span>, 0<span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 循环取出记录 何问起</span> <span style="color: #0000ff;">while</span> (<span style="color: #800080;">$row</span>=<span style="color: #008080;">mysql_fetch_row</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">echo</span> "<tr>/n"<span style="color: #000000;">; </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$i</span>=0; <span style="color: #800080;">$i</span>mysql_num_fields(<span style="color: #800080;">$result</span>); <span style="color: #800080;">$i</span>++<span style="color: #000000;"> ) { </span><span style="color: #0000ff;">echo</span> '<td bgcolor="#00FF00">'<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$row</span>[<span style="color: #800080;">$i</span>]"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> '</td>'<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "</tr>/n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "</table>/n"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "</font>"<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;"> 释放资源 hovertree.com</span> <span style="color: #008080;">mysql_free_result</span>(<span style="color: #800080;">$result</span><span style="color: #000000;">); </span><span style="color: #008000;">//</span><span style="color: #008000;"> 关闭连接</span> <span style="color: #008080;">mysql_close</span><span style="color: #000000;">(); </span>?>
下面是运行结果:
pi_id pi_name pi_tel pi_qq pi_email
1 Zhangsan 13911111111 642864125 zhangsan@126.com
2 Lisi 13122222222 63958741 lisi@163.com
3 Wangwu 13833333333 912345678 wangwu@sohu.com
所谓“万变不离其宗”,再复杂的操作也都是基于上面的来的,都少不了上面的基本的步骤,当需要时查一下相关的手册便可以解决。
附参考代码:
<h1>插入操作</h1> <span style="color: #000000;">php </span><span style="color: #0000ff;">if</span>(!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['submit'<span style="color: #000000;">])){ </span><span style="color: #008000;">//</span><span style="color: #008000;">如果没有表单提交,显示一个表单 </span> ?>php } else { //如果提交了表单 hovertree.com //数据库连接参数 $host = "localhost"; $user = "root"; $pass = "zq19890319"; $db = "phpdev"; //取得表单中的值,检查表单中的值是否符合标准,并做适当转义,防止SQL注入 $country = empty($_POST['country'])? die("请输入国家名称"): mysql_escape_string($_POST['country']); $animal = empty($_POST['animal'])? die("请输入英文名"): mysql_escape_string($_POST['animal']); $cname = empty($_POST['cname'])? die("请输入中文名"): mysql_escape_string($_POST['cname']); //打开数据库连接 何问起 $connection = mysql_connect($host, $user, $pass) or die("Unable to connect!"); //选择数据库 mysql_select_db($db) or die("Unable to select database!"); //构造一个SQL查询 $query = "INSERT INTO symbols(country, animal, cname) VALUE('$country', '$animal', '$cname')"; //执行该查询 $result = mysql_query($query) or die("Error in query: $query. ".mysql_error()); //插入操作成功后,显示插入记录的记录号 echo "记录已经插入, mysql_insert_id() = ".mysql_insert_id(); //关闭当前数据库连接 mysql_close($connection); } ?>