㈠ 如何用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>
<th>Username</th>
<th>Password</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>
<title>FORM</title>
</head>
<body>
<br />
<h1>Insert:</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 />
<h1>Delete</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 />
<h1>Update</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
㈡ 如何在PHPCMS的后台代码中实现增删改查
$this->db->update(array('is_login'=>0),array('username'=>$username));
㈢ 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封装一个类能实现mysql数据库的增删改查
还没到20个问题吗
㈤ php 如何操作access数据库, 关于增删改查的四种操作
resourse odbc_connect( string dsn, string user, string password [, int cursor_type])dsn:系统dsn名称user:数据库服务器某用户名。password:数据库服务器某用户密码。 php程序员站cursor_type:游标类型。这样连接上access的数据库增删改查就是标准的sql语句了,楼主应该没什么问题了吧
㈥ php怎么链接sqlserver数据库进行增删改查
php有专门的sql server操作函数,举个简单的例子,是查询的
$serverName="localhost";//数据库服务器地址
$uid="root";//数据库用户名
$pwd="123456";//数据库密码
$connectionInfo=array("UID"=>$uid,"PWD"=>$pwd,"Database"=>'databasename');
$conn=sqlsrv_connect($serverName,$connectionInfo);
if($conn==false){
echo"连接数据库失败!";
die(print_r(sqlsrv_errors(),true));
}
$sql="select*fromuser";
$query=sqlsrv_query($conn,$sql,array(),array("Scrollable"=>SQLSRV_CURSOR_KEYSET));
$num_rows=sqlsrv_num_rows($query);
if($num_rows>0){
while($row=sqlsrv_fetch_array($query)){
echo$row['aaaa'];
}
}
其它的操作也同理,举一反三
㈦ php 如何操作access数据库, 关于增删改查的四种操作
resourse
odbc_connect(
string
dsn,
string
user,
string
password
[,
int
cursor_type])
dsn:系统dsn名称
user:数据库服务器某用户名。
password:数据库服务器某用户密码。
php程序员站
cursor_type:游标类型。
这样连接上access的数据库
增删改查就是标准的sql语句了,楼主应该没什么问题了吧
㈧ 求phpcms v9的数据库增删改查 是怎么实现的
phpcms v9是基于mvc模式开发的,所以我们按照其套路到模型层程序中去找就行。在/phpcms/model目录下存放着与数据表名称一致的模型成文件,随意打开一个,你会看到都继承了model这个类,那么找到它,路径:/phpcms/libs/classes/model.class.php。对于数据库的增删改查方法都在这里面了,列举如下:
insert() 增加数据、delete()删除指定条件数据、listinfo()读取支持翻页的多条数据、select()读取多条数据、update()更新数据。
当然,还有很多方法,以及各方法的传参各代表什么意义都有详细的注释看看就会明白的。
那么我们在控制器中应该如何引入一个数据表的model并对其进行数据操作呢,例如我在首页控制器中获取最近注册的10个会员账号信息,可以这么写:
$member_db=pc_base::load_model("members_model");
<!--使用pc_base的load_model方法进行加载指定的数据表模型,感觉像TP3.2里的M()函数-->
$member_list=$member_db->select(array('islock'=>0),"*",10,"iddesc");
<!--使用对应的方法获取数据-->
就演示到这里吧,如果有mvc架构基础应该一看就懂的,更多的关于phpcms的二次开发深入可以参考官方开发手册、代码中的注释说明以及iphpcms里的二次开发视频教程。
㈨ php 使用pdo对mysql数据库增删改查实例
//连接、查询
try
{
$dbh
=
new
PDO('mysql:host=localhost;dbname=test',
$user,
$pass);
foreach($dbh->query('SELECT
*
from
FOO')
as
$row)
{
print_r($row);
}
$dbh
=
null;//释放资源
}
catch
(PDOException
$e)
{
print
"错误!:
"
.
$e->getMessage()
.
"<br></br>";
die();
}
//执行删除操作,插入、修改与此类似,更改SQL语句即可
$count
=
$dbh->exec("DELETE
FROM
fruit
WHERE
colour
=
'red'");
㈩ thinkphp3.2.3版本的数据库增删改查实现代码
框架thinkphp
版本:3.2.3
内容:数据库操作
1.
多表查找一条数据
M('a表')->join("b表
on
b表.id=a表.id")->where('条件')->find();
2.查找一条数据
M('a表')->where('条件')->find();
3.多表查询所有数据
M('a表')->join("b表
on
b表.id=a表.id")->where('条件')->select();
4.查询所有数据
M('a表')->where('条件')->select();
5.增加一条数据
M('a表')->add($data);
6.删除一条数据
M('a表')->where('条件')->delete($data);
7.修改一条数据
M('a表')->where('id=5')->save();
以上就是小编为大家带来的thinkphp3.2.3版本的数据库增删改查实现代码的全部内容了,希望对大家有所帮助,多多支持脚本之家~