php实现数据库操作 php操作mysql数据库
php实现数据库处理
?php
我们提供的服务有:成都网站建设、做网站、微信公众号开发、网站优化、网站认证、绥德ssl等。为上1000家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的绥德网站制作公司
$title = $_POST['title'];
$name = $_POST['author'];
$message = $_POST['my_message'];
$dbhost = '127.0.0.1';
$dbuser = 'root'; //我的用户名
$dbpass = ''; //我的密码
$dbname = 'exer'; //我的mysql库名
$connect = mysql_connect($dbhost,$dbuser,$dbpass,$dbname);
mysql_query("set names 'utf8" );
mysql_query("INSERT INTO message VALUES (null,'".$title."','".$name."','".$message."',null)")or die("query error");
echo "留言成功";
echo "script type='text/javascript' alert('留言成功')";
echo "window.location='allmessage.php';/script";
?
这是正确的 ,你的"" '' 什么的很乱 ,你自己试着调调
php怎么操作数据库
//链接数据库
$db=mysql_connect("localhost", "root","123456");
$sqlname="database";
mysql_select_db($sqlname,$db);
session_start();
mysql_query("SET NAMES 'utf8'",$db);
?
?
//创建结果集
$sql = "SELECT * FROM kecheng";
$result = mysql_query($sql);
$rs= mysql_fetch_array($result);
?
?
//修改数据
$sql = "UPDATE news SET title='1111' where news_id=1";
$result = mysql_query($sql);
?
?
//删除数据
$sql = "DELETE from news where news_id=1";
$result = mysql_query($sql);
?
?
//添加数据
$sql = "INSERT INTO news (title,fenshu) VALUES ('a',1)"
$result = mysql_query($sql);
?
PHP基于单例模式实现的数据库操作基类
本文实例讲述了PHP基于单例模式实现的数据库操作基类。分享给大家供大家参考,具体如下:
配置文件:
?php
$db
=
array(
'host'='localhost',
'user'='root',
'password'='',
'database'='test',
)
?
php
数据库基类:
?php
class
db
{
public
$conn;
public
static
$sql;
public
static
$instance=null;
private
function
__construct(){
require_once('db.config.php');
$this-conn
=
mysql_connect($db['host'],$db['user'],$db['password']);
if(!mysql_select_db($db['database'],$this-conn)){
echo
"失败";
};
mysql_query('set
names
utf8',$this-conn);
}
public
static
function
getInstance(){
if(is_null(self::$instance)){
self::$instance
=
new
db;
}
return
self::$instance;
}
/**
*
查询数据库
*/
public
function
select($table,$condition=array(),$field
=
array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$fieldstr
=
'';
if(!empty($field)){
foreach($field
as
$k=$v){
$fieldstr.=
$v.',';
}
$fieldstr
=
rtrim($fieldstr,',');
}else{
$fieldstr
=
'*';
}
self::$sql
=
"select
{$fieldstr}
from
{$table}
{$where}";
$result=mysql_query(self::$sql,$this-conn);
$resuleRow
=
array();
$i
=
0;
while($row=mysql_fetch_assoc($result)){
foreach($row
as
$k=$v){
$resuleRow[$i][$k]
=
$v;
}
$i++;
}
return
$resuleRow;
}
/**
*
添加一条记录
*/
public
function
insert($table,$data){
$values
=
'';
$datas
=
'';
foreach($data
as
$k=$v){
$values.=$k.',';
$datas.="'$v'".',';
}
$values
=
rtrim($values,',');
$datas
=
rtrim($datas,',');
self::$sql
=
"INSERT
INTO
{$table}
({$values})
VALUES
({$datas})";
if(mysql_query(self::$sql)){
return
mysql_insert_id();
}else{
return
false;
};
}
/**
*
修改一条记录
*/
public
function
update($table,$data,$condition=array()){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
$updatastr
=
'';
if(!empty($data)){
foreach($data
as
$k=$v){
$updatastr.=
$k."='".$v."',";
}
$updatastr
=
'set
'.rtrim($updatastr,',');
}
self::$sql
=
"update
{$table}
{$updatastr}
{$where}";
return
mysql_query(self::$sql);
}
/**
*
删除记录
*/
public
function
delete($table,$condition){
$where='';
if(!empty($condition)){
foreach($condition
as
$k=$v){
$where.=$k."='".$v."'
and
";
}
$where='where
'.$where
.'1=1';
}
self::$sql
=
"delete
from
{$table}
{$where}";
return
mysql_query(self::$sql);
}
public
static
function
getLastSql(){
echo
self::$sql;
}
}
$db
=
db::getInstance();
//$list
=
$db-select('demo',array('name'='tom','password'='ds'),array('name','password'));
//echo
$db-insert('demo',array('name'='脚本之家','password'='123'));
//echo
$db-update('demo',array("name"='xxx',"password"='123'),array('id'=1));
echo
$db-delete('demo',array('id'='2'));
db::getLastSql();
echo
"pre";
?
更多关于PHP操作数据库相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《PHP基于pdo操作数据库技巧总结》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
php opencart怎么进行数据库操作
1,根目录下config.php中定义了数据库信息:
// DB
define('DB_DRIVER', 'mysqli');
define('DB_HOSTNAME', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'qqqq');
define('DB_DATABASE', 'qqqq');
define('DB_PORT', '3306');
define('DB_PREFIX', 'oc_');
2,systemlibrary文件夹下db.php中定义了DB类,数据库的连接方法
$this-db = new $class($hostname, $username, $password, $database, $port);
3,index.php中初始化数据库连接
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_PORT);
4,controller中调用model层定义的查询函数
$layout_id = $this-model_catalog_category-getCategoryLayoutId(end($path));
5,model层getCategoryLayoutId函数中返回查询sql结果
$query = $this-db-query("SELECT * FROM " . DB_PREFIX . "category_to_layout WHERE category_id = '" . (int)$category_id . "' AND store_id = '" . (int)$this-config-get('config_store_id') . "'");
php怎么操作数据库~呢?
PHP链接数据库有几种方式
mysqli:
?php
$servername = "localhost";
$username = "username";
$password = "password";
// 创建连接
$conn = new mysqli($servername, $username, $password);
// 检测连接
if ($conn-connect_error) {
die("连接失败: " . $conn-connect_error);
}
echo "连接成功";
?
也可以使用PDO进行链接,前提是你必须在php.ini中开启PDO:
?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
echo "连接成功";
}
catch(PDOException $e)
{
echo $e-getMessage();
}
?
建议使用PDO,功能更加强大,兼容各种数据库
关于这个问题,差不多就是这个样子的了,你如果不明白,可以自己去后盾瞅瞅,我这些都是在后盾上学的,有空可以去看一下,就算不喜欢也没关系啊,何乐而不为呢?
文章标题:php实现数据库操作 php操作mysql数据库
网址分享:http://azwzsj.com/article/hephgj.html