當前位置:首頁 » 編程語言 » php資料庫語句

php資料庫語句

發布時間: 2024-04-05 06:02:46

php中插入Mysql資料庫的語句怎麼寫

顯示資料庫或表:
showdatabases;//然後可以usedatabase_name;
showtables;
更改表名:
altertabletable_namerenamenew_t;
添加列:
altertabletable_nameaddcolumnc_ncolumnattributes;
刪除列:
altertabletable_namedropcolumnc_n;
創建索引:
altertablec_tableaddindex(c_n1,c_n2);
altertablec_tableadniqueindex_name(c_n);
altertablec_tableaddprimarykey(sid);
刪除索引:
altertablec_tabledropindexc_n1;
更改列信息:
altertablet_tablechangec_1c_1varchar(200);
altertablet_tablemodify1c_1varchar(200);
insert插入語句:
insertintotable_name(c_1,c_2)
values('x1',1);
update語句:
updatetable_namesetc_1=1wherec_2=3;
刪除資料庫或者表:
droptabletable_name;
dropdatabasedatabase_name;//使用mysql_drop_db()可以刪除的.

❷ php資料庫添加、刪除、修改數據(mysql)

一、PHP操作MySql資料庫
新增數據
?php
$query
=
"INSERT
INTO
grade
(name,email,point,regdate)
VALUE
('
李三','[email protected]',,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語句,怎麼從資料庫中隨機獲取數據欄位。

//連接資料庫
if(!$con = mysql_connect("localhost","root","root")){die(mysql_error());}

mysql_select_db("ali_xt");
mysql_query('set names utf8');

//找出ali_admin表的欄位
$res = mysql_query('show columns from ali_admin');

//將數據給弄出來
$data = array();
while ($row = mysql_fetch_assoc($res, MYSQL_NUM)) {
$data[] = $row;
}

//隨機個數,默認5
$rand_times = 5;
$rand_times = count($data)<$rand_times?count($data):$rand_times;

$result = array();
for( $i=0;$i<$rand_times;$i++ ){
$result[] = $data[rand(0,count($data)-1)][0];
}

echo "<pre>";
print_r($result); //輸出5個隨機欄位

mysql_close($con);

//純手寫的,不明白可以問我,記得給分

❹ thinkphp鎬庝箞鎵撳嵃sql璇鍙

thinkphp濡備綍鎵撳嵃sql璇鍙

ThinkPHP鐨剆ql璇鍙ュ簲鐢:

[SQL]鏄劇ず鈥渢hink_action鈥濅腑鐨勫垪[榪愯屾椂:0.001339s]

[瑙i噴:鏁扮粍(id1,select_type綆鍗,琛=think_action鍒嗗尯

=綾誨瀷=鎵鏈,鍙鑳界殑閽ュ寵=keykey_lenref琛屾暟=82,榪囨護=100.00,extra鏁版嵁綾誨瀷>:[鍒楃駭瀹屾暣鎬х害鏉焆]

[錛岃〃鍒楃駭瀹屾暣鎬х害鏉焆)錛

鎻忚堪:

1銆<>鏄瘲QL璇鍙ヤ腑蹇呴』瀹氫箟鐨勯儴鍒嗭紝[]鏄瘲QL璇鍙ョ殑鍙閫夐儴鍒嗭紝鍙浠ョ渷鐣ャ

2.CREATETABLE鏄瘲QL鐨勫叧閿瀛楋紝琛ㄧず璇SQL璇鍙ョ殑鍔熻兘銆

3銆<Tablename>:鏄瑕佸畾涔夌殑鍩烘湰琛ㄧ殑鍚嶇О銆備竴涓琛ㄥ彲浠ョ敱涓涓鎴栧嚑涓灞炴(鍒)緇勬垚錛屼絾鑷沖皯鏈変竴涓灞炴с備笉鍏佽告病鏈変換浣曞睘鎬х殑琛ㄣ傝繖涓嶆槸絀鴻〃鐨勬剰鎬濄傚氫釜灞炴у畾涔夌殑杈圭晫鐢ㄦ嫭鍙瘋〃紺猴紝姣忎釜灞炴у畾涔夌敤閫楀彿鍒嗛殧銆傛瘡涓灞炴х殑鍚嶇О浜掍笉鐩稿悓錛屽苟涓斿彲浠ユ寜浠繪剰欏哄簭鎺掑垪銆備竴鑸鎸夌収瀹炰綋鎴栬仈緋諱漢瀹氫箟鐨勫睘鎬х殑欏哄簭鎺掑垪錛屽叧閿瀛楀睘鎬х粍鍦ㄦ渶鍓嶉潰錛屼究浜庡尯鍒嗭紝闃叉㈤仐婕忓畾涔夌殑灞炴с

鏉ユ簮:鐧懼害鐧劇-thinkphp

鍙傝冩潵婧:鐧懼害鐧劇-緇撴瀯鍖栨煡璇㈣璦

鏂版墜涓婅礬奼傚姪錛孋I妗嗘灦涓嶆墦鍗版棩蹇楋紵

絳旀堬細瀹屽叏鍙浠r棣栧厛濡傛灉鏄鏈鍦拌嚜宸變嬌鐢ㄧ殑璇濓紝寤鴻浣跨敤鍘熺敓php鍐欙紙鍙闄愯嚜宸變嬌鐢錛屽傛灉浣犳槸澶х墰錛屽畨鍏ㄦу緢鑷淇¢櫎澶栵級r鍏舵℃兂蹇閫熷苟涓斿湪瀹夊叏涓婂己浜庤嚜宸辯殑璇濆氨浣跨敤妗嗘灦錛孋I鎴栬呭叾浠栫殑laravel錛宼hinkphp絳夌瓑閮芥槸鍙浠ョ殑r

❺ php批量執行sql語句怎麼寫

php中利用數組用Mysql_query批量執行SQL語句。
參考示例如下:
思路:這里採用一個數組.用explode 函數,將$query語句按照」;」炸開,然後循環執行即可:
$query = 'delete from ecs_goods_attr where attr_id=11 and goods_id=22;
Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,33,138,"膽略",0);
update ecs_goods set goods_number=10,shop_price=55 where goods_id=33;'
$query_e = explode(';','$query');
foreach ($query_e as $k =>$v)
{
mysql_query($query_e[$k]);
}
這樣 $query語句就被批量的執行了。

❻ php資料庫操作類中的$sql語句是什麼語句

這個不用實例化

你只要實例化了資料庫類就行了

我讀了你這個類,$sql在裡面應該是SQL語句的意思,$sql只是類裡面的形式參數。
實際你只要傳遞一個值進去就行了。

比如:
<?php
$db=new mysql('localhost','root','','test','')
//實例化mysql類。第五參數我看不懂,可能這類有點小問題的
$db->query("select * from test");
//訪問query函數,參數select * from test
.....
?>
這個類不好。我給你一個把:
<?php
class mysql{

private $db_host; //資料庫主機
private $db_user; //資料庫用戶名
private $db_pwd; //資料庫用戶名密碼
private $db_database; //資料庫名
private $conn; //資料庫連接標識;
private $result; //執行query命令的結果資源標識
private $sql; //sql執行語句
private $row; //返回的條目數
private $coding; //資料庫編碼,GBK,UTF8,gb2312
private $bulletin = true; //是否開啟錯誤記錄
private $show_error = true; //測試階段,顯示所有錯誤,具有安全隱患,默認關閉
private $is_error = false; //發現錯誤是否立即終止,默認true,建議不啟用,因為當有問題時用戶什麼也看不到是很苦惱的

/*構造函數*/
function __construct($db_host,$db_user,$db_pwd,$db_database,$conn,$coding){
$this->db_host=$db_host;
$this->db_user=$db_user;
$this->db_pwd = $db_pwd;
$this->db_database=$db_database;
$this->conn=$conn;
$this->coding=$coding;
$this->connect();
}

/*資料庫連接*/
function connect()
{
if($this->conn=="pconn"){
//永久鏈接
$this->conn=mysql_pconnect($this->db_host,$this->db_user,$this->db_pwd);
}else{
//即使鏈接
$this->conn=mysql_connect($this->db_host,$this->db_user,$this->db_pwd);
}

if(!mysql_select_db($this->db_database,$this->conn)){
if($this->show_error){
$this->show_error("資料庫不可用:",$this->db_database);
}
}
mysql_query("SET NAMES $this->coding");
}

/*資料庫執行語句,可執行查詢添加修改刪除等任何sql語句*/
function query($sql)
{
if($sql == ""){
$this->show_error("sql語句錯誤:","sql查詢語句為空");}
$this->sql = $sql;

$result = mysql_query($this->sql,$this->conn);

if(!$result){
//調試中使用,sql語句出錯時會自動列印出來
if($this->show_error){
$this->show_error("錯誤sql語句:",$this->sql);
}
}else{
$this->result = $result;
}
return $this->result;
}

/*創建添加新的資料庫*/
function create_database($database_name){
$database=$database_name;
$sqlDatabase = 'create database '.$database;
$this->query($sqlDatabase);
}

/*查詢伺服器所有資料庫*/
//將系統資料庫與用戶資料庫分開,更直觀的顯示?
function show_databases(){
$this->query("show databases");
echo "現有資料庫:".$amount =$this->db_num_rows($rs);
echo "<br />";
$i=1;
while($row = $this->fetch_array($rs)){
echo "$i $row[Database]";
echo "<br />";
$i++;
}
}

//以數組形式返回主機中所有資料庫名
function databases()
{
$rsPtr=mysql_list_dbs($this->conn);
$i=0;
$cnt=mysql_num_rows($rsPtr);
while($i<$cnt)
{
$rs[]=mysql_db_name($rsPtr,$i);
$i++;
}
return $rs;
}

/*查詢資料庫下所有的表*/
function show_tables($database_name){
$this->query("show tables");
echo "現有資料庫:".$amount = $this->db_num_rows($rs);
echo "<br />";
$i=1;
while($row = $this->fetch_array($rs)){
$columnName="Tables_in_".$database_name;
echo "$i $row[$columnName]";
echo "<br />";
$i++;
}
}

/*
mysql_fetch_row() array $row[0],$row[1],$row[2]
mysql_fetch_array() array $row[0] 或 $row[id]
mysql_fetch_assoc() array 用$row->content 欄位大小寫敏感
mysql_fetch_object() object 用$row[id],$row[content] 欄位大小寫敏感
*/

/*取得結果數據*/
function mysql_result_li()
{
return mysql_result($str);
}

/*取得記錄集,獲取數組-索引和關聯,使用$row['content'] */
function fetch_array()
{
return mysql_fetch_array($this->result);
}

//獲取關聯數組,使用$row['欄位名']
public function fetch_assoc()
{
return mysql_fetch_assoc($this->result);
}

//獲取數字索引數組,使用$row[0],$row[1],$row[2]
function fetch_row()
{
return mysql_fetch_row($this->result);
}

//獲取對象數組,使用$row->content
function fetch_Object()
{
return mysql_fetch_object($this->result);
}

//簡化查詢select
function findall($table)
{
$this->query("SELECT * FROM $table");
}

//簡化查詢select
function select($table,$columnName,$condition)
{
if($columnName==""){
$columnName="*";
}

$this->query("SELECT $columnName FROM $table $condition");

}

//簡化刪除del
function delete($table,$condition){
$this->query("DELETE FROM $table WHERE $condition");
}

//簡化插入insert
function insert($table,$columnName,$value){
$this->query("INSERT INTO $table ($columnName) VALUES ($value)");
}

//簡化修改update
function update($table,$mod_content,$condition){
$this->query("UPDATE $table SET $mod_content WHERE $condition");
}

/*取得上一步 INSERT 操作產生的 自動增長的ID*/
function insert_id(){
return mysql_insert_id();
}

//指向確定的一條數據記錄
function db_data_seek($id){
if($id>0){
$id=$id-1;
}
if(!@mysql_data_seek($this->result,$id)){
$this->show_error("sql語句有誤:", "指定的數據為空");
}
return $this->result;
}

// 根據select查詢結果計算結果集條數
function db_num_rows(){
if($this->result==null){

if($this->show_error){
$this->show_error("sql語句錯誤","暫時為空,沒有任何內容!");
}
return -1;
}else{
return mysql_num_rows($this->result);
}
}

// 根據insert,update,delete執行結果取得影響行數
function db_affected_rows(){
return mysql_affected_rows();
}

//輸出顯示sql語句
function show_error($message="",$sql=""){
if(!$sql){
echo "<font color='red'>".$message."</font>";
echo "<br />";
}else{
echo "<fieldset>";
echo "<legend>錯誤信息提示:</legend><br />";
echo "<div style='font-size:14px; clear:both; font-family:Verdana, Arial, Helvetica, sans-serif;'>";
echo "<div style='height:20px; background:#000000; border:1px #000000 solid'>";
echo "<font color='white'>錯誤號:12142</font>";
echo "</div><br />";
echo "錯誤原因:".mysql_error()."<br /><br />";
echo "<div style='height:20px; background:#FF0000; border:1px #FF0000 solid'>";
echo "<font color='white'>".$message."</font>";
echo "</div>";
echo "<font color='red'><pre>".$sql."</pre></font>";
$ip=$this->getip();
if($this->bulletin){
$time = date("Y-m-d H:i:s");
$message=$message."\r\n$this->sql"."\r\n客戶IP:$ip"."\r\n時間 :$time"."\r\n\r\n";

$server_date=date("Y-m-d");
$filename=$server_date.".txt";
$file_path="error/".$filename;
$error_content=$message;
//$error_content="錯誤的資料庫,不可以鏈接";
$file = "error"; //設置文件保存目錄

//建立文件夾
if(!file_exists($file)){
if(!mkdir($file,0777)){
//默認的 mode 是 0777,意味著最大可能的訪問權
die("upload files directory does not exist and creation failed");
}
}

//建立txt日期文件
if(!file_exists($file_path)){

//echo "建立日期文件";
fopen($file_path,"w+");

//首先要確定文件存在並且可寫
if (is_writable($file_path))
{
//使用添加模式打開$filename,文件指針將會在文件的開頭
if (!$handle = fopen($file_path, 'a'))
{
echo "不能打開文件 $filename";
exit;
}

//將$somecontent寫入到我們打開的文件中。
if (!fwrite($handle, $error_content))
{
echo "不能寫入到文件 $filename";
exit;
}

//echo "文件 $filename 寫入成功";

echo "——錯誤記錄被保存!";

//關閉文件
fclose($handle);
} else {
echo "文件 $filename 不可寫";
}

}else{
//首先要確定文件存在並且可寫
if (is_writable($file_path))
{
//使用添加模式打開$filename,文件指針將會在文件的開頭
if (!$handle = fopen($file_path, 'a'))
{
echo "不能打開文件 $filename";
exit;
}

//將$somecontent寫入到我們打開的文件中。
if (!fwrite($handle, $error_content))
{
echo "不能寫入到文件 $filename";
exit;
}

//echo "文件 $filename 寫入成功";
echo "——錯誤記錄被保存!";

//關閉文件
fclose($handle);
} else {
echo "文件 $filename 不可寫";
}
}

}
echo "<br />";
if($this->is_error){
exit;
}
}
echo "</div>";
echo "</fieldset>";

echo "<br />";
}

//釋放結果集
function free(){
@mysql_free_result($this->result);
}

//資料庫選擇
function select_db($db_database){
return mysql_select_db($db_database);
}

//查詢欄位數量
function num_fields($table_name){
//return mysql_num_fields($this->result);
$this->query("select * from $table_name");
echo "<br />";
echo "欄位數:".$total = mysql_num_fields($this->result);
echo "<pre>";
for ($i=0; $i<$total; $i++){
print_r(mysql_fetch_field($this->result,$i) );
}
echo "</pre>";
echo "<br />";
}

//取得 MySQL 伺服器信息
function mysql_server($num=''){
switch ($num){
case 1 :
return mysql_get_server_info(); //MySQL 伺服器信息
break;

case 2 :
return mysql_get_host_info(); //取得 MySQL 主機信息
break;

case 3 :
return mysql_get_client_info(); //取得 MySQL 客戶端信息
break;

case 4 :
return mysql_get_proto_info(); //取得 MySQL 協議信息
break;

default:
return mysql_get_client_info(); //默認取得mysql版本信息
}
}

//析構函數,自動關閉資料庫,垃圾回收機制
function __destruct()
{
if(!empty($this->result)){
$this->free();
}
//echo "對象被釋放";
mysql_close($this->conn);
}//function __destruct();

/*獲得客戶端真實的IP地址*/
function getip(){
if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
{
$ip = getenv("HTTP_CLIENT_IP");
}
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
{
$ip = getenv("REMOTE_ADDR");
}
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
$ip = $_SERVER['REMOTE_ADDR'];
}
else{
$ip = "unknown";
}
return($ip);
}

}

?>

調用辦法:和上面差不多
<?php
$db=new mysql('localhost','root','','test',"conn",'gbk');
//實例化mysql類。主機名localhost,用戶名root,密碼空(''),資料庫test,連接方式普通連接(conn,pconn是永久鏈接),字元集gbk
$db->query("select * from test");
//訪問query函數。
echo $db->db_num_rows();
//訪問db_num_rows函數。顯示結果行數(因為類能有跟蹤功能)
?>

❼ 使用php語句將資料庫*.sql文件導入資料庫

最簡單的php語句把資料庫*.sql文件導入資料庫
復制代碼
代碼如下:
$sql=file_get_contents("text.sql");
//把SQL語句以字元串讀入$sql
$a=explode(";",$sql);
//用explode()函數把‍$sql字元串以「;」分割為數組
foreach($a
as
$b){
//遍歷數組
$c=$b.";";
//分割後是沒有「;」的,因為SQL語句以「;」結束,所以在執行SQL前把它加上
mysql_query($c);
//執行SQL語句
}
在很多安裝程序中大家都會看到資料庫的安裝,其它神秘的背後就是這么簡單的幾句代碼。首先安裝中會有一個Sql文件,然後再按照上面的代碼在PHP中執行。

熱點內容
微信牆完美版php源碼 發布:2024-05-01 20:17:36 瀏覽:482
優酷上傳視頻軟體 發布:2024-05-01 20:17:32 瀏覽:269
vc與資料庫連接 發布:2024-05-01 20:16:37 瀏覽:997
天貓在哪裡修改密碼 發布:2024-05-01 19:58:59 瀏覽:734
安卓手機文件怎麼收藏 發布:2024-05-01 19:50:22 瀏覽:333
mysql存儲過程ifnull 發布:2024-05-01 19:27:11 瀏覽:254
台州華為雲存儲伺服器 發布:2024-05-01 19:12:10 瀏覽:882
短租系統源碼 發布:2024-05-01 18:59:11 瀏覽:636
深度優先演算法廣度優先演算法 發布:2024-05-01 18:47:02 瀏覽:437
pythonlist索引獲取 發布:2024-05-01 18:43:20 瀏覽:153