$db_host = "localhost";//链接的数据库地址,也就是主机名字
$db_user = "db";//数据库名字
$db_pass = "数据库密码";
$db_name = "msg";//表名
$connec = mysql_connect($db_host,$db_user,$db_pass) or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db($db_name,$connec) or die ("不能选择数据库: ".mysql_error());
$user=$_POST['user']; //$_post不用大写的就没用得
$sms=$_POST['sms'];
$ID=$_POST['id'];
$db_query='INSERT INTO msg(表名) VALUES $user,$sms,$ID';//插入
mysql db query($db_query);//运行sql语句
上面的程序改改就可以用了,或许有问题,我在网吧,没调试的!
我也是学PHP的,现在还很菜,有时间的话咱交流交流!
举例如下:
创建userinfo_update.php页面用于查询用户信息,先显示信息,在修改:
先通过GET获取用户编号查询用户信息:
$sql = "select * from user_info where user_id='".$_GET['userId']."'";
$result = mysql_query($sql,$con);
if($row = mysql_fetch_array($result)){
}
页面效果:
创建update.php文件,用于修改用户信息:
使用到了mysql_affected_rows() 函数返回前一次 MySQL 操作所影响的记录行数.
//通过post获取页面提交数据信息
$userId = $_POST[userId];
$userName = $_POST[userName];
$userAge = $_POST[userAge];
$sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'";
mysql_query($sql,$conn);//执行SQL
$mark? = mysql_affected_rows();//返回影响行数
$url = "userinf_select.php";
运行结果
创建delete.php文件,完成删除用户信息功能:
$userId = $_GET['userId'];
include 'connection.php';
$sql = "delete from user_info where user_id='".$userId."'";
mysql_query($sql,$con);
if($mark0){
echo "删除成功";
}else{
echo? "删除失败";
mysql_close($con);
运行结果:
php
class MySQL{
private $host; //服务器地址
private $name; //登录账号
private $pwd; //登录密码
private $dBase; //数据库名称
private $conn; //数据库链接资源
private $result; //结果集
private $msg; //返回结果
private $fields;//返回字段
private $fieldsNum; //返回字段数
private $rowsNum; //返回结果数
private $rowsRst; //返回单条记录的字段数组
private $filesArray = array();//返回字段数组
private $rowsArray = array();//返回结果数组
private $query_count=0; //查询结果次数
static private $_instance; //存储对象
//初始化类
private function __construct($host='',$name='',$pwd='',$dBase=''){
if($host != '') $this-host = $host;
if($name != '') $this-name = $name;
if($pwd != '') $this-pwd = $pwd;
if($dBase != '') $this-dBase = $dBase;
$this-init_conn();
//防止被克隆
private function __clone(){}
public static function getInstance($host='',$name='',$pwd='',$dBase=''){
if(FALSE == (self::$_instance instanceof self)){
self::$_instance = new self($host,$name,$pwd,$dBase);
return self::$_instance;
public function __set($name,$value){
$this-$name=$value;
public function __get($name){
return $this-$name;
//链接数据库
function init_conn(){
@mysql_select_db($this-dBase,$this-conn) or die('select db fail !');
mysql_query("set names ".$this-charset);
//查询结果
function mysql_query_rst($sql){
if($this-conn == '') $this-init_conn();
$this-result = @mysql_query($sql,$this-conn);
$this-query_count++;
//取得字段数
function getFieldsNum($sql){
$this-mysql_query_rst($sql);
$this-fieldsNum = @mysql_num_fields($this-result);
//取得查询结果数
function getRowsNum($sql){
if(mysql_errno() == 0){
return @mysql_num_rows($this-result);
return '';
//取得记录数组(单条记录)
function getRowsRst($sql,$type=MYSQL_BOTH){
if(empty($this-result)) return '';
if(mysql_error() == 0){
$this-rowsRst = mysql_fetch_array($this-result,$type);
return $this-rowsRst;
//取得记录数组(多条记录)
function getRowsArray($sql,$type=MYSQL_BOTH){
!empty($this-rowsArray) ? $this-rowsArray=array() : '';
while($row = mysql_fetch_array($this-result,$type)) {
$this-rowsArray[] = $row;
return $this-rowsArray;
//更新、删除、添加记录数
function uidRst($sql){
if($this-conn == ''){
@mysql_query($sql);
$this-rowsNum = @mysql_affected_rows();
return $this-rowsNum;
//返回最近插入的一条数据库的id值
function returnRstId($sql){
return mysql_insert_id();
//获取对应的字段值
function getFields($sql,$fields){
if(mysql_num_rows($this-result) 0){
$tmpfld = @mysql_fetch_row($this-result);
$this-fields = $tmpfld[$fields];
return $this-fields;
//错误信息
function msg_error(){
if(mysql_errno() != 0) {
$this-msg = mysql_error();
return $this-msg;
//释放结果集
function close_rst(){
mysql_free_result($this-result);
$this-msg = '';
$this-fieldsNum = 0;
$this-rowsNum = 0;
$this-filesArray = '';
$this-rowsArray = '';
//关闭数据库
function close_conn(){
$this-close_rst();
mysql_close($this-conn);
$this-conn = '';
//取得数据库版本
function db_version() {
return mysql_get_server_info();
php操作mysql步骤:
or
die('数据库连接失败.'mysql_error());链接mysql.
names
=
"select
*
from
blog_article";准备要查询的数据.
mysql_query($sql);执行sql查询.
mysql_fetch_assoc($datas)得到查询到的缓存在内存中的一条数据.
相同点:三个函数都是返回数据库中查询到的一行数据(说的再清楚点就是一条数据).
不同点:mysql_fetch_assoc()用的是数据库中相应的字段名作为的key值(也就是数组下标)
如:filed['id']=1;
mysql_fetch_row()用的是自动生成的数字(从0开始依次生成)作为的key值(也就是数组下标)
如:filed[0]=1;
mysql_fetch_array()用的是自动生成的数字(从0开始依次生成)作为的key值(也就是数组下标),而且它还同时生成数据库中相应的字段名作为的key值(也就是数组下标)
如:
filed[0]=1,filed['id']=1;也就是说,mysql_fetch_array()将mysql_fetch_assoc()和mysql_fetch_row()查询到的结果合为了一体了.
mysql_fetch_object()与mysql_fetch_assoc()差不多.只是mysql_fetch_assoc()返回的是数组.mysql_fetch_object()返回的是object对象.
mysql_insert_id() 取得上一步
INSERT
操作产生的
ID.
mysql_result()
函数返回结果集中一个字段的值.
mysql_num_fields()
函数返回结果集中字段的数目.
mysql_affected_rows();返回前一次
MySQL
操作所影响的记录行数.
mysql_num_rows(mysql_query($sql))获得结果集中行的数目.
mysql_pconnect()
函数打开一个到
服务器的持久连接.
和
mysql_connect()
非常相似,但有两个主要区别:
当连接的时候本函数将先尝试寻找一个在同一个主机上用同样的用户名和密码已经打开的(持久)连接,如果找到,则返回此连接标识而不打开新连接.
其次,当脚本执行完毕后到
SQL
服务器的连接不会被关闭,此连接将保持打开以备以后使用(mysql_close()
不会关闭由
建立的连接).
mysql_unbuffered_query($sql)和mysql_query($sql)效果差不多,但是
mysql_unbuffered_query($sql)不缓存.mysql_query($sql)会缓存查询的结果.
mysql_close();关闭mysql的最近的链接.
primary_key
auto_increment
.
mysql_fetch_lengths(mysql_query($sql))返回该条数据的所有字段的每个字段的长度.返回的是一个数字组成的数组.
mysql_field_table(mysql_query($sql),0)返回指定字段所在的表名.
mysql_free_result(mysql_query($sql))
函数释放结果内存.
mysql_get_client_info()
函数返回
mysql_get_host_info()
取得
主机信息.
以上就是土嘎嘎小编为大家整理的php操作数据库code相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!