Home > Backend Development > PHP Tutorial > PHP中mysql数据库基础操作

PHP中mysql数据库基础操作

WBOY
Release: 2016-06-20 13:04:31
Original
937 people have browsed it

PHP中mysql数据库基础操作

当前通过PHP+Mysql组合来开发网站是主流趋势,通过PHP语言来操作mysql数据库是一件很简单的事情,下面我们来了解下怎样通过php操作数据库。

一 、创建数据表

<p>create table user1(</p> id int primary key auto_increment,<br /> name varchar(32) not null,<br /> password varchar(64) not null,<br /> email varchar(128) not null,<br /> age unsigned tinyint not null<br /><p> )</p>
Copy after login

二 、给数据库添加数据

insert into user1(name,password,email,age) value ('zs',md5('12345'),'zs@163.com',30);
Copy after login

三 、php操作mysql数据库语法

1、连接mysql数据库
$conn=mysql_connect("127.0.0.1","root","901822");<br />if(!$conn){<br />die("连接失败".mysql_error());<br />}else{<br /> echo "成功";<br /><p>}
Copy after login

2、选择数据库

mysql_select_db("test");
Copy after login

3、设置编码

mysql_query("set names utf8");
Copy after login

4、执行sql语句

$sql="select * from user1";<br />$sql="insert into user1(name,password,email,age) values('小明',md5('123'),'xiaoming@163.com',34)";<br />$res=mysql_equery($sql,$conn);
Copy after login


5、接收返回结果

while($row=mysql_fetch_row($res)){<br /> echo "<br/>$row[0]--$row[1]--$row[2]";<br /> echo "<br/>";<br /> var_dump($row);<br />}
Copy after login

还有一种取法

<p>$row=mysql_fetch_array($res);<br /></p>foreach($row as $key=>$val){<br /> echo "--$val";<br /> }<br /> echo "<br/>";<br />}<br />
Copy after login


6、释放资源

mysql_free_result($res);<br /><p>mysql_close($conn);</p>
Copy after login

释放资源$result,连接数据库资源$conn,尽早关闭


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template