41. PHP MySQL 连接数据库
来源:互联网 发布:计算圆周率算法 编辑:程序博客网 时间:2024/06/01 20:17
连接到一个 MySQL 数据库
在您能够访问并处理数据库中的数据之前,您必须创建到达数据库的连接。
在 PHP 中,这个任务通过 mysql_connect() 函数完成。
语法
mysql_connect(servername,username,password);
<?php$con = mysql_connect("localhost","peter","abc123");if (!$con) { die('Could not connect: ' . mysql_error()); }// some code?>
关闭连接
脚本一结束,就会关闭连接。如需提前关闭连接,请使用 mysql_close() 函数。
mysql_close($con);
1.查询操作header("Content-Type:text/html;charset=utf-8");// 1.获取连接$link = mysql_connect('localhost','root','123456');if(!$link){ die("连接错误" . mysql_error());}else{ echo 'Ok' . '<br/>';}//2. 选择数据库mysql_select_db('test',$link);//3. 设置操作编码//保证php程序是按照utf8码操作mysql_query("set names utf8");//4. 发送指令$sql = 'select * from user1';//$result 表示结果集,可以理解为是一张表,返回的结果集放在内存中$result = mysql_query($sql,$link);//5. 接收返回结果,并处理//mysql_fetch_row 会依次取出结果集$result的下一行数据复制给$row//$row 是一个数组while($row = mysql_fetch_row($result)){ //echo "<br/> $row[0] --- $row[1] --- $row[2]"; foreach($row as $k=>$v){ echo $k . '---' . $v ; } echo "<br/>";}//6. 释放资源,即释放内存mysql_free_result($result);//7. 关闭连接,这句话可以没有,但建议有。有的话也要过几分钟关闭mysql_close($link);
2. 插入,删除,更新header("Content-Type:text/html;charset=utf-8");$link = mysql_connect('localhost','root','123456');if(!$link){ die('cuo le' . mysql_error());}mysql_select_db('test',$link) or die(mysql_error());mysql_query('set names utf8');$sql = "insert into user11(name,password,email,age) values('xiaoming',md5('123546'),'xiaoming@qq.com',20)";//删除操作//$sql = "delete from user1 where id=5";//更新操作//$sql = "update user1 set age = 100 where id = 4";//如果是dml操作,则返回bool$result = mysql_query($sql,$link);if(!$result){ echo "操作失败" . mysql_error(); die();}//看看有几条数据if(mysql_affected_rows($link)>0){ echo "操作成功";}else{ echo "没有影响到行数";}//加上这句话会报错,因为返回的是bool值//mysql_free_result($result);mysql_close($link);
封装工具类:
<?php/** * Created by PhpStorm. * User: Hua * Date: 2015/10/18 * Time: 15:18 */class SqlTool{ private $link; private $host = 'localhost'; private $user = 'root'; private $password = '123456'; private $db = "test"; public function __construct() { $this->link = mysql_connect($this->host,$this->user,$this->password); if(!$this->link){ die("连接失败" . mysql_error()); } mysql_select_db($this->db,$this->link); mysql_query("set names utf8"); } //完成select public function execute_dql($sql) { $result = mysql_query($sql) or die(mysql_error()); return $result; } //完成update ,delete, insert public function execute_dml($sql) { $b = mysql_query($sql,$this->link); if(!$b){ return 0;//失败 }else{ if(mysql_affected_rows($this->link)>0){ return 1;//表示成功 }else{ return 2;//表示没有行数影响 } } mysql_close($this->link); }}
<?phpheader("Content-Type:text/html;charset=utf-8");require_once "SqlTool.class.php";//*************************dml********************************/*$sql = "insert into user1(name,password,email,age) values('xiaoming',md5('123'),'xiao@qq.com',200)";$obj = new SqlTool();$res = $obj->execute_dml($sql);if($res == 0){ echo "失败";}else if($res == 1){ echo "成功";}else if($res == 2){ echo "没有影响";}*///************************dql********************$sql = "select * from user1";$obj = new SqlTool()();$result = $obj->execute_dql($sql);while($row = mysql_fetch_row($result)){ foreach ($row as $k=>$v) { echo '---$v'; } echo "<br/>";}mysql_free_result($result);
2. 在线词典
mainView.php<html><head> <title>在线词典</title> <meta http-equiv="content-type" content="text/html;charset=utf-8"/></head><body><h1>查询英文</h1><form action="wordProcess.php" method="post"> 请输入英文:<input type="text" name="enword"/> <input type="hidden" value="search" name="type"/> <input type="submit" value="查询"/></form></body></html>
wordProcess.php<?php // 接收英文单词require_once 'SqlTool.php';header("Content-type:text/html;charset=utf-8");if(isset($_POST['enword'])){ $en_word = $_POST['enword'];}else{ echo "输入为空"; echo "<a href='mainView.php'>返回重新查询</a>>";}//看看数据库中有没有这条记录$sql = "select chword from words where enword='" . $en_word . "' limit 0,1";//设计表//查询,可以使用工具类$sqlTool = new SqlTool();$res = $sqlTool->execute_dql($sql);if($row = mysql_fetch_array($res)){ echo $en_word . '对应的中文是' . $row['chword'];}else{ echo '没有这个词条'; echo "<br/><a href='mainView.php'>重新返回查询</a>";}mysql_free_result($res);
工具类:SqlTool.php<?phpclass SqlTool{ private $link; private $host = 'localhost'; private $user = 'root'; private $password = '123456'; private $db = "test"; public function __construct() { $this->link = mysql_connect($this->host,$this->user,$this->password); if(!$this->link){ die("连接失败" . mysql_error()); } mysql_select_db($this->db,$this->link); mysql_query("set names utf8"); } //完成select public function execute_dql($sql) { $result = mysql_query($sql) or die(mysql_error()); return $result; } //完成update ,delete, insert public function execute_dml($sql) { $b = mysql_query($sql,$this->link); if(!$b){ return 0;//失败 }else{ if(mysql_affected_rows($this->link)>0){ return 1;//表示成功 }else{ return 2;//表示没有行数影响 } } mysql_close($this->link); }}
0 0
- 41. PHP MySQL 连接数据库
- PHP连接MYSQL数据库
- php连接mysql数据库
- php连接mysql数据库
- PHP 连接MySQL 数据库
- php连接mysql数据库
- PHP连接MYSQL数据库
- php连接mysql数据库
- PHP MySQL 连接数据库
- PHP连接MYSQL数据库
- PHP连接MYSQL数据库
- php连接mysql数据库
- PHP连接Mysql数据库
- PHP连接MySQL数据库
- PHP连接mysql数据库
- PHP连接MySQL数据库
- php连接mysql数据库
- PHP连接Mysql数据库
- Java – Check if key exists in HashMap
- Android自定义解析Html的TextView-HtmlView
- 程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大系列集锦
- js实现用户输入的小写字母自动转大写字母
- svn本地资源异常
- 41. PHP MySQL 连接数据库
- C++ string::push_back
- Swift数据类型-可选类型
- 虚拟机VMware Workstation 提示cannot connect to the virtual machine.
- nginx配置proxy_pass代理转发
- Jquery 全选/反选
- 华为机试——输出指定字段
- 我的博客,开始启程(改变始于意识)
- 进程线程及堆栈关系的总结