search
Homephp教程php手册PHP读取MySQL数据代码

PHP读取MySQL数据代码

Jun 13, 2016 pm 12:29 PM
linkmmysqlphpforcodecontentcopydatadocumentNewread

  新建view.php文件,其内容为: 

复制代码 代码如下:


$link=mysql_connect("localhost","root","之前的管理员密码");  
if(!$link) echo "没有连接成功!";  
else echo "连接成功!";  
?> 


    这样,我们便连接上了MySQL数据库,接下来,就是数据读取部分的代码,根据我的MySQL数据源表结构的介绍,我们试验性的来读取:

    depart     varchar(45)     部门名称
    ename     varchar(45)     员工姓名
    pcname   varchar(45)     PC名称

这三段数据。

    在view.php中紧接着前边的代码继续写: 

复制代码 代码如下:


mysql_select_db("infosystem", $link);          //选择数据库 
$q = "SELECT * FROM info";                   //SQL查询语句 
mysql_query("SET NAMES GB2312");          
$rs = mysql_query($q, $link);                     //获取数据集 
if(!$rs){die("Valid result!");} 
echo "

"; 
echo ""; 
while($row = mysql_fetch_row($rs)) echo "";   //显示数据 
echo "
部门名称 员工姓名 PC名称
$row[1] $row[2] $row[3]
"; 
mysql_free_result($rs);                    //关闭数据集 
?> 
    其中,“infosystem”为数据库名,“info”为表名。大家可以根据自己的情况酌情修改。

    接着,运行它~怎么样~数据是不是顺利被读取了呢?

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool