목차
插入操作
php教程 php手册 PHP+MySql例子 - roucheng

PHP+MySql例子 - roucheng

May 20, 2016 am 10:14 AM

对于熟悉做网站的人来说,要想网站做成动态的,肯定要有数据库的支持,利用特定的脚本连接到数据库,从数据库中提取资料、向数据库中添加资料、删除资料等。这里我通过一个实例来说明如何用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>
?>  
    
로그인 후 복사
国家: 动物名称(英文): 动物名称(中文):
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); } ?>

推荐:http://www.cnblogs.com/roucheng/p/3528396.html

본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)