mysql工_mysql
命令行:
查詢當(dāng)前數(shù)據(jù)庫(kù)字符編碼格式? show variables like 'character%';
創(chuàng)建數(shù)據(jù)庫(kù) create database message;
刪除數(shù)據(jù)庫(kù)? drop database message;
查詢所有數(shù)據(jù)庫(kù) show databases;
選擇數(shù)據(jù)庫(kù)? use message;
在sql 語(yǔ)句里直接寫(xiě)變量是可以的 但是如果變量是個(gè)數(shù)組 必須用{} 包含變量 大括號(hào)外可以再用單引號(hào) 但是最外側(cè)必須用雙引號(hào)? sql語(yǔ)句用雙引號(hào)包含
$sql = "insert into tg_user(uniqid,active,username,password,email,qq,url,sex,face,re_time,last_login_time,last_login_ip) values('{$post_array['uniqid']}','{$post_array['active']}','{$post_array['username']}','{$post_array['pswd']}','{$post_array['email']}','{$post_array['qq']}','{$post_array['www']}','{$post_array['sex']}','{$post_array['face1']}',NOW(),NOW(),'{$_SERVER['REMOTE_ADDR']}')";
最后登錄ip? {$_SERVER['REMOTE_ADDR']}
創(chuàng)建數(shù)據(jù)表
mysql> create table users(
-> id integer primary key auto_increment ,
-> name varchar(50),
-> pswd varchar(50)
-> );
查詢數(shù)據(jù)表
show tables;
增
insert into users values(1,'long','123456');
查
select * from users;
phpmyadmin
時(shí)間類(lèi)型:
類(lèi)型: timestamp ? 默認(rèn)值 CURRENT_TIMESTAMP
屬性: ON UPDATE CURRENT_TIMESTAMP
(修改或插入數(shù)據(jù)時(shí)時(shí)間會(huì)自動(dòng)改變)
CURRENT_TIMESTAMP
當(dāng)要向數(shù)據(jù)庫(kù)執(zhí)行insert操作時(shí),如果有個(gè)timestamp字段屬性設(shè)為
CURRENT_TIMESTAMP,則無(wú)論這個(gè)字段有沒(méi)有set值都插入當(dāng)前系統(tǒng)時(shí)間
ON UPDATE CURRENT_TIMESTAMP
當(dāng)執(zhí)行update操作是,并且字段有ON UPDATE CURRENT_TIMESTAMP屬性。則字段無(wú)論值有沒(méi)有變化,它的值也會(huì)跟著更新為當(dāng)前UPDATE操作時(shí)的時(shí)間。
創(chuàng)建數(shù)據(jù)庫(kù)時(shí)需要選擇數(shù)據(jù)庫(kù)編碼
創(chuàng)建字段時(shí)需要選擇表編碼
導(dǎo)出數(shù)據(jù)庫(kù)?? 導(dǎo)入數(shù)據(jù)庫(kù)
添加enum 類(lèi)型字段時(shí)? 默認(rèn)值不要引號(hào)
檢測(cè)php是否支持mysql鏈接
通過(guò)phpinfo.php配置文件? 查看三個(gè)位置? mysql? mysqlli? pdo_mysql
查看mysqlsupport 為 enabled 則支持mysql數(shù)據(jù)庫(kù)
若不支持需修改php.ini? 配置文件
1.?? 查看 extension_dir? 是否是擴(kuò)展庫(kù)目錄
2.打開(kāi)三個(gè)配置文件
extension? php_mysql.dll???? 面向過(guò)程? 使用php的函數(shù)連接mysql
extension? php_mysqlli.dll??? 面向?qū)ο?? 使用對(duì)象和方法實(shí)現(xiàn)連接mysql
extension? php_pdo_mysql.dll?? 抽象層數(shù)據(jù)庫(kù)連接mysql
面向過(guò)程連接數(shù)據(jù)庫(kù)
基本流程:
1. 建立數(shù)據(jù)庫(kù)連接
2.選擇數(shù)據(jù)庫(kù)
3.設(shè)置數(shù)據(jù)庫(kù)字符編碼
4.執(zhí)行sql語(yǔ)句
5.處理查詢結(jié)果
6.關(guān)閉數(shù)據(jù)庫(kù)連接
設(shè)置模板文件:
數(shù)據(jù)庫(kù)配置文件db_config.php
$db_info = array(
'db_host' => '127.0.0.1',
'db_user' => 'root',
'db_pswd' => 'root',
'db_name' => 'message',
'db_charset' => 'utf8',
'db_port' => '3306',
);
?>
數(shù)據(jù)庫(kù)連接文件 db_connect.php
require_once('db_config.php');
$link = mysql_connect($db_info['db_host'].':'.$db_info['db_port'],$db_info['db_user'],$db_info['db_pswd']);
if(!$link){
die("數(shù)據(jù)庫(kù)連接失敗".mysql_error());
}
mysql_select_db($db_info['db_name']) or die('數(shù)據(jù)庫(kù)選擇失敗'.mysql_error());
mysql_set_charset($db_info['db_charset']);
?>
操作文件? text.php
require_once('db_connect.php');
$sql = 'select * from message';
$result = mysql_query($sql);????? // 執(zhí)行sql語(yǔ)句 沒(méi)有數(shù)據(jù)也返回的是true??? sql語(yǔ)句錯(cuò)誤返回false
var_dump($result);
$lines = mysql_num_rows($result);? //返回查詢的結(jié)果條數(shù)
var_dump($lines);
$row = mysql_fetch_row($result);? // 從結(jié)果集中取出一條數(shù)據(jù)作為索引數(shù)組? 若結(jié)果集為空則返回false
var_dump($row);
for ($i=0; $i < $lines; $i++) {? //循環(huán)取出結(jié)果集 索引數(shù)組
$row = mysql_fetch_row($result);
var_dump($row);
}
while($row = mysql_fetch_row($result)){?? //循環(huán)取出結(jié)果集? 大部分是用這種方式取出結(jié)果集 索引數(shù)組
var_dump($row);
}
while($row = mysql_fetch_assoc($result)){?? //返回關(guān)聯(lián)數(shù)組? 這個(gè)用的比較多
var_dump($row);
}
mysql_close($link);
?>
總結(jié)
以上是生活随笔為你收集整理的mysql工_mysql的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql数据库21_MySQL数据库技
- 下一篇: mysql load data 语法_M