PHP+MySql例子 - roucheng
对于熟悉做网站的人来说,要想网站做成动态的,肯定要有数据库的支持,利用特定的脚本连接到数据库,从数据库中提取资料、向数据库中添加资料、删除资料等。这里我通过一个实例来说明如何用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 id="插入操作">插入操作</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> ?>

Alat AI Hot

Undresser.AI Undress
Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover
Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool
Gambar buka pakaian secara percuma

Clothoff.io
Penyingkiran pakaian AI

AI Hentai Generator
Menjana ai hentai secara percuma.

Artikel Panas

Alat panas

Notepad++7.3.1
Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina
Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1
Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6
Alat pembangunan web visual

SublimeText3 versi Mac
Perisian penyuntingan kod peringkat Tuhan (SublimeText3)
