search
Homephp教程php手册详解PHP显示MySQL数据的三种方法

详解PHP显示MySQL数据的三种方法

Jun 13, 2016 pm 12:29 PM
linkmysqlphpdatamethodyesterdayyesshowofprogramDetailed explanation

  昨天的程序是这样的:
$link=mysql_connect("localhost","root","之前的管理员密码"); 
if(!$link) echo "没有连接成功!"; 
else echo "连接成功!"; 
mysql_select_db("infosystem", $link); 
$q = "SELECT * FROM info";                 
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_close($link);
?>
    程序中红色部分就是显示MySQL中数据的关键。这里,大家可以用其它两种方式替换红色部分并达到相同效果。

while($row = mysql_fetch_object($rs)) echo " $row->depart $row->ename $row-pcname "; 
while($row = mysql_fetch_array($rs)) echo " $row[depart] $row[ename] $row[pcname] ";
    简要地说明一下:

    用mysql_fetch_row()的方法返回的一行数据列储存在一个数组的单元中,偏移量从 0 开始。例如你要返回第二个字段的数据,就应该写成$row[1]而不是$row[2]。

    mysql_fetch_array() 和 mysql_fetch_row() 类似,返回的数据也是存储在一个数组中,有所区别的是我们应该用字段命来访问该数组而不是通过偏移量,例如$row[depart]。

    mysql_fetch_object()返回的不再是数组而是一个对象,我们应该用对对象的操作方法来读取数据,例如:$row->depart。

    大家可以根据自身需要,选择一种方式,代替程序中红色部分来读取MySQL数据。
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 Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.