本文目录一览:
- 1、php连接mysql的增删查改和连接的代码
- 2、php封装一个class类实现mysql数据库的增删该查
- 3、php封装一个class类,实现mysql数据库的增删改查怎么操做?
- 4、php数据库添加、删除、修改数据(mysql)
- 5、如何用PHP代码实现MySQL数据库的增删改查
php连接mysql的增删查改和连接的代码
$mysql_server_name="localhost"; //数据库服务器名称
$mysql_username="root"; // 连接数据库用户名
$mysql_password="??????"; // 连接数据库密码
$mysql_database="??????"; // 数据库的名字
// 连接到数据库
$conn=mysql_connect($mysql_server_name, $mysql_username,
$mysql_password);
// 从表中提取信息的sql语句
$strsql="SELECT * FROM `gbook`";
// 执行sql查询
$result=mysql_db_query($mysql_database, $strsql, $conn);
// 获取查询结果
$row=mysql_fetch_row($result);
增删该查,只要sql语句的区别,只要修改mysql_query($sql)的$sql参数。
php封装一个class类实现mysql数据库的增删该查
?php
class db{
private $db;
const MYSQL_OPT_READ_TIMEOUT = 11;
const MYSQL_OPT_WRITE_TIMEOUT = 12;
private $tbl_name;
private $where;
private $sort;
private $fields;
private $limit;
public static $_instance = null;
function __construct(){
$cfg = loadConfig('db');
$db = mysqli_init();
$db-options(self::MYSQL_OPT_READ_TIMEOUT, 3);
$db-options(self::MYSQL_OPT_WRITE_TIMEOUT, 1);
@$db-real_connect($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);
if ($db-connect_error) {
$this-crash($db-errno,$db-error);
}
$db-set_charset("utf8");
$this-db = $db;
//echo $this-db-stat;
}
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self();
}
return self::$_instance;
}
private function __clone() {} //覆盖__clone()方法,禁止克隆
public function find($conditions = null){
if($conditions) $this-where($conditions);
return $this-getArray($this-buildSql(),1);
}
public function findAll($conditions = null){
if($conditions) $this-where($conditions);
return $this-getArray($this-buildSql());
}
//表
public function t($table){ $this-tbl_name = $table; return $this;}
//条件
public function where($conditions){
$where = '';
if(is_array($conditions)){
$join = array();
foreach( $conditions as $key = $condition ){
$condition = $this-db-real_escape_string($condition);
$join[] = "`{$key}` = '{$condition}'";
}
$where = "WHERE ".join(" AND ",$join);
}else{
if(null != $conditions) $where = "WHERE ".$conditions;
}
$this-where = $where;
return $this;
}
//排序
public function sort($sort){
if(null != $sort) $sort = "ORDER BY {$sort}";
$this-sort = $sort;
return $this;
}
//字段
public function fields($fields){ $this-fields = $fields; return $this; }
public function limit($limit){$this-limit = $limit; return $this;}
private function buildSql(){
$this-fields = empty($this-fields) ? "*" : $this-fields;
$sql = "SELECT {$this-fields} FROM {$this-tbl_name} {$this-where} {$this-sort}";
accessLog('db_access',$sql);
if(null != $this-limit)$sql .= " limit {$this-limit}";
return $sql;
}
/**
* 返回查询数据
* @param $sql
* @param bool $hasOne
* @return array|bool|mixed
*/
private function getArray($sql,$hasOne = false){
if($this-db-real_query($sql) ){
if ($result = $this-db-use_result()) {
$row = array();
if($hasOne){
$row = $result-fetch_assoc();
}else{
while($d = $result-fetch_assoc()) $row[] = $d;
}
$result-close();
$this-fields = "*";
return $row;
}else{
return false;
}
}else{
if($this-db-error){
$this-crash($this-db-errno,$this-db-error,$sql);
}
}
}
public function findSql($sql,$hasOne = false){
accessLog('db_access',$sql);
if($this-db-real_query($sql) ){
if ($result = $this-db-use_result()) {
$row = array();
if($hasOne){
$row = $result-fetch_assoc();
}else{
while($d = $result-fetch_assoc()) $row[] = $d;
}
$result-close();
$this-fields = "*";
return $row;
}else{
return false;
}
}else{
if($this-db-error){
$this-crash($this-db-errno,$this-db-error,$sql);
}
}
}
public function create($row){
if(!is_array($row))return FALSE;
$row = $this-prepera_format($row);
if(empty($row))return FALSE;
foreach($row as $key = $value){
$cols[] = '`'.$key.'`';
$vals[] = "'".$this-db-real_escape_string($value)."'";
}
$col = implode(',', $cols);
$val = implode(',', $vals);
$sql = "INSERT INTO `{$this-tbl_name}` ({$col}) VALUES ({$val})";
accessLog('db_access',$sql);
if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID
if($this-db-insert_id){
return $this-db-insert_id;
}
if($this-db-affected_rows){
return true;
}
}
return FALSE;
}
//直接执行sql
public function runSql($sql){
accessLog('db_access',$sql);
if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID
return true;
}else{
return false;
}
}
public function update($row){
$where = "";
$row = $this-prepera_format($row);
if(empty($row))return FALSE;
foreach($row as $key = $value){
$value = $this-db-real_escape_string($value);
$vals[] = "`{$key}` = '{$value}'";
}
$values = join(", ",$vals);
$sql = "UPDATE {$this-tbl_name} SET {$values} {$this-where}";
accessLog('db_access',$sql);
if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID
if( $this-db-affected_rows){
return true;
}
}
return false;
}
function delete(){
$sql = "DELETE FROM {$this-tbl_name} {$this-where}";
if( FALSE != $this-db-query($sql) ){ // 获取当前新增的ID
if( $this-db-affected_rows){
return true;
}
}
return FALSE;
}
private function prepera_format($rows){
$columns = $this-getArray("DESCRIBE {$this-tbl_name}");
$newcol = array();
foreach( $columns as $col ){
$newcol[$col['Field']] = $col['Field'];
}
return array_intersect_key($rows,$newcol);
}
//崩溃信息
private function crash($number,$message,$sql=''){
$msg = 'Db Error '.$number.':'.$message ;
if(empty($sql)){
echo t('db_crash');
}else{
$msg .= " SQL:".$sql;
echo t('db_query_err');
}
accessLog('db_error',$msg);
exit;
}
}
php封装一个class类,实现mysql数据库的增删改查怎么操做?
class sqlHelper{
public $conn;
public $dbname="数据库名称";
public $username="数据库用户名";
public $password="数据库密码";
public $host="localhost";
//连接数据库
public function __construct(){
$this-conn=mysql_connect($this-host,$this-username,$this-password);
if(!$this-conn){
die("连接失败".mysql_error());
}
mysql_select_db($this-dbname,$this-conn);
}
//执行查询语句
public function execute_dql($sql){
$res=mysql_query($sql,$this-conn);
return $res;
}
//执行增填改语句
public function execute_dml($sql){
$b=mysql_query($sql,$this-conn);
if(!$b){
return 3;
}else{
if(mysql_affected_rows($this-conn)){
return 1;//表示OK
}else{
return 2;//表示没有行收到影响
}
}
}
}
php数据库添加、删除、修改数据(mysql)
一、PHP操作MySql数据库
新增数据
?php
$query
=
"INSERT
INTO
grade
(name,email,point,regdate)
VALUE
('
李三','yc60.com@gmail.com',,NOW())"
;
@mysql_query($query)
or
die(
'添加数据出错:'
.mysql_error());
?
修改数据
?php
$query
=
"UPDATE
grade
SET
name='小可爱'
WHERE
id=6"
;
@mysql_query($query)
or
die(
'修改出错:'
.mysql_error());
?
删除数据
?php
$query
=
"DELETE
FROM
grade
WHERE
id=6";
@mysql_query($query)
or
die(
'删除错误:'
.mysql_error());
?
显示数据
?php
$query
=
"SELECT
id,name,email,point
FROM
grade";
$result
=
@mysql_query($query)
or
die(
'查询语句出错:'
.mysql_error());
while
(!!
$row
=
mysql_fetch_array($result))
{
echo
$row[
'id'
].
'----'
.$row['name'
].'----'
.$row
['email'
].
'----'
.$row['point'
];
echo
'br
/
';
}
?
二、其他常用函数
mysql_f
etch_row()
:从结果集中取得一行作为枚举数组
mysql_f
etch_assoc()
:
从结果集中取得一行作为关联数组
mysql_f
etch_array()
:
从结果集中取得一行作为关联数组,或数字数组,或二者兼有
mysql_f
etch_lengths
()
:
取得结果集中每个输出的长度
mysql_f
ield_name():
取得结果中指定字段的字段名
mysql_num_rows():
取得结果集中行的数目
mysql_num_f
ields():取得结果集中字段的数目
mysql_get_client_inf
o()
:
取得
MySQL
客户端信息
mysql_get_host_info():
取得
MySQL
主机信息
mysql_get_proto_info():
取得
MySQL
协议信息
mysql_get_server_inf
o()
:
取得
MySQL
服务器信息
如何用PHP代码实现MySQL数据库的增删改查
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM user");
echo "table border='1'
tr
thUsername/th
thPassword/th
/tr";
while($row = mysql_fetch_array($result)) {
echo "tr";
echo "td" . $row['username'] . "/td";
echo "td" . $row['password'] . "/td";
echo "/tr";
}
echo "/table";
mysql_close($con);
?
从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");
mysql_close($con);
?
删除该用户所有信息delete.php
?php
$con = mysql_connect("localhost:3306","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$sql = "INSERT INTO user (username,password)
VALUES
('$_POST[username]','$_POST[password]')";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?
注册一个新用户insert.php
?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");
mysql_close($con);
?
修改一个用户密码update.php
html
head
titleFORM/title
/head
body
br /
h1Insert:/h1
form action="insert.php" method="post"
username:input type="name" name="username"/
br /
password:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
h1Delete/h1
form action="delete.php" method="post"
username:input type="name" name="username" /
br /
Are you sure?input type="submit" value="sure" /
/form
br /hr /br /
h1Update/h1
form action="update.php" method="post"
username:input type="name" name="username"/
br /
You want to change your password into:input type="password" name="password"/
input type="submit" value="submit"/
/form
br /hr /br /
/body
/html
以上三个功能的提交源Operate.html