search
Homephp教程php手册PHP访问MySql数据库 初级篇

PHP访问MySql数据库 初级篇

Jun 06, 2016 pm 07:54 PM
mysqlphpprimaryBackstagedatabasewebsitewantaccess

在网站后台,经常要与数据库打交道。本文介绍如何使用XAMPP来管理MySql数据库及如何用PHP来访问MySql数据库。 一.使用XAMPP来管理MySql数据库 首先使用XAMPP打开MySql的管理页面。步骤如下:启动XAMPP后点击Admin进入XAMPP for Windows的主页面,在主页面中

在网站后台,经常要与数据库打交道。本文介绍如何使用XAMPP来管理MySql数据库及如何用PHP来访问MySql数据库。

一.使用XAMPP来管理MySql数据库

首先使用XAMPP打开MySql的管理页面。步骤如下:启动XAMPP后点击Admin进入XAMPP for Windows的主页面,在主页面中点击phpMyAdmin。

PHP访问MySql数据库 初级篇

 

进入phpMyAdmin页面后,新建数据库test并在此数据库中建立t_student表,表共三个字段,编号id,姓名name,年龄age。

PHP访问MySql数据库 初级篇

然后就可以开始用PHP来访问MySql数据库了。由于PHP已经对访问MySql数据库作了良好的封装,因此用PHP访问MySql是一件非常容易的事情。

 

二.PHP访问MySql数据库

下面的PHP程序访问test数据库中t_student表,读取数据并以表格的形式输出数据。全部程序代码如下:

// by MoreWindows( http://blog.csdn.net/MoreWindows )
//定义常量
define(DB_HOST, 'localhost');
define(DB_USER, 'root');
define(DB_PASS, '111111');
define(DB_DATABASENAME, 'test');
define(DB_TABLENAME, 't_student');
//数据库表的列名
$dbcolarray = array('id', 'name', 'age');

//mysql_connect
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("connect failed" . mysql_error());
mysql_select_db(DB_DATABASENAME, $conn);

//读取表中纪录条数
$sql = sprintf("select count(*) from %s", DB_TABLENAME);
$result = mysql_query($sql, $conn);
if ($result)
{
	$count = mysql_fetch_row($result);
}
else
{
	die("query failed");
}
echo "表中有$count[0] 条记录<br>";


$sql = sprintf("select %s from %s", implode(",",$dbcolarray), DB_TABLENAME);
$result = mysql_query($sql, $conn);
//表格
echo '
'; //表头 $thstr = ""; echo $thstr; //表中的内容 while ($row=mysql_fetch_array($result, MYSQL_ASSOC))//与$row=mysql_fetch_assoc($result)等价 { echo ""; $tdstr = ""; foreach ($dbcolarray as $td) $tdstr .= ""; echo $tdstr; echo ""; } echo "
" . implode(" ", $dbcolarray) . "
$row[$td]
"; mysql_free_result($result); mysql_close($conn);

运行结果如下:

PHP访问MySql数据库 初级篇

这种HTML元素全由PHP输出的方式比较原始,将介绍用smarty来完成读取数据库的任务并将《jquery 表格的增加删除和修改及设置奇偶行颜色》中的设置表格奇偶行颜色的功能加入。

 

 

http://blog.csdn.net/morewindows/article/details/7086524

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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),

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment