『壹』 php 表單怎樣執行sql語句
if($_GET['id']=='del'):
$del="delete from kbxx";
$res=mysql_query($del,$con);
exit;
endif;
echo'<a href="a.php?id=del">刪除</a>';
『貳』 如何在php中執行多條sql語句
這里沒有很官方的解釋,我個人認為mysql_query 不能批量執行SQL語句的原因最主要的一個就是Mysql_query不能判斷你的批量語句裡面是否包含錯誤.為了最大的保證程序的順利執行,所以,乾脆罷工了.
解決的辦法,有很多種.這里僅列出我的心得之一(利用數組用Mysql_query批量執行SQL語句)
$query = 『delete from ecs_goods_attr where attr_id=138 and goods_id=442;Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,442,138,」歐版 白色」,0);update ecs_goods set goods_number=10,shop_price=955 where goods_id=442;』
$query 是我需要執行的SQL語句,顯然這里 mysql_query($query); 是無法得到我們想要的結果的.這里我們採用一個數組.用explode 函數,將$query語句按照」;」炸開.這個說的比較形象.呵呵
$query = 『delete from ecs_goods_attr where attr_id=138 and goods_id=442;Insert into ecs_goods_attr (goods_attr_id,goods_id,attr_id,attr_value,attr_price)values(Null,442,138,」歐版 白色」,0);update ecs_goods set goods_number=10,shop_price=955 where goods_id=442;』
$query_e = explode(『;』,』$query』);
foreach ($query_e as $k =>$v)
{
mysql_query($query_e[$k]);
}
這樣 $query語句就被批量的執行了.呵呵
『叄』 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查詢怎麼做
$haha = M(),$res = $haha->query($sql)。
或 $res = $waw->execute($sql)。
$sql中包含了表名,實例化模型時可以為空。注意query是查功能,execute是增刪改功能。
『伍』 php怎麼樣連接sql資料庫
1、軟體配置
Win7 64 +wampserver2.2d-x32+SQL Server 2008 R2資料庫,wamp2.2中的php版本是5.3.10。
Php環境也可以換成php+apache。
2、支持連接MySQL Server配置
php版本5.3以前,有php_mssql功能,可以使用,但是5.3及以後的版本不支持。
2.1、php連接mssql設置(php5.3以前版本)
(1)、打開php.ini,將
;extension=php_mssql.dll前面的分號(;)去掉,然後重啟 Apache。如果不行的話,進行第2步。
(2)檢查一下你的php安裝目錄下的ext下面有沒有php_mssql.dll存在,如果沒有,從www.php.net重新下載一個php安裝,要下載那個壓縮包的才是最完整的。
如果ext目錄下已經有了php_mssql.dll,那麼你需要打開php.ini,找到
extension_dir = "./ext"
這一句(或者類似的,不一定是"./ext",查找"extension_dir"即可),然後把"./ext"修改為你的php安裝目錄的ext目錄的完整路徑,比如"c:/php/ext",或者"c:/program files/php/ext"這樣。然後再次重啟 Apache。如果還是不行的話,可能就需要第3步了。
(3)把 php 目錄下的 ntwdblib.dll 和 php_mssql.dll 復制到 system32的系統目錄中去,然後重起Apache。
(4)然後就可以連接MSSQL,並進行一些操作了。連接例子如下:
2.2、php連接sqlsrv(php5.3以及以上版本)
(1)、下載Microsoft Drivers forPHPfor SQL Server,官方下載地址:http://www.microsoft.com/en-us/download/details.aspx?id=20098,我使用的是SQLSRV2.0。
(2)、解壓下載下來的文件將php_pdo_sqlsrv_53_ts_vc9.dll文件和php_sqlsrv_53_ts_vc9.dll文件復制到php安裝目錄下的ext文件夾中。此處根據版本不同使用的文件不同。
(3)、在php.ini中添加
extension=php_sqlsrv_53_ts_vc9.dll
extension=php_pdo_sqlsrv_53_ts_vc9.dll
到很多;extension=***.dll語句後面,注意extension_dir 指向的位置是否正確。
(4)、重啟apache,然後訪問http://apache訪問地址/?Phpinfo=1,出現下面圖片中的內容,則代表配置正確。
(5)、寫測試代碼,測試代碼如下:
(6)、上面完成之後,測試代碼的時候會出現連接失敗,因為沒有安裝sql server nation client ,本地客戶端,去下載合適的客戶端,我使用的是Microsoft? SQL Server? 2012 Native Client,下載地址:
(7)、安裝完成之後,在重新啟動apache,然後訪問就可以連接成功了。
(8)、注意:在php.ini文件所在的文件夾中必須要有ntwdblib.dll文件存在。
『陸』 如何利用PHP執行.SQL文件
以下代碼來自PHPBB的SQL文件解析類,原理就是逐行的解釋SQL文件,並進行刪除等操作,可以參照修改。希望能幫到你。
<?php
ini_set('memory_limit','5120M');
set_time_limit(0);
/***************************************************************************
*sql_parse.php
*-------------------
*begin:ThuMay31,2001
*right:(C)2001ThephpBBGroup
*email:[email protected]
*
*$Id:sql_parse.php,v1.82002/03/1823:53:12psotfxExp$
*
****************************************************************************/
/***************************************************************************
*
*Thisprogramisfreesoftware;youcanredistributeitand/ormodify
*
*theFreeSoftwareFoundation;eitherversion2oftheLicense,or
*(atyouroption)anylaterversion.
*
***************************************************************************/
/***************************************************************************
*
*_utilitiesundertheadmin
*,specifically
*
*functionsintothisfile.JLH
*
***************************************************************************/
//
//remove_
//....
//
functionremove_comments(&$output)
{
$lines=explode(" ",$output);
$output="";
//trytokeepmem.usedown
$linecount=count($lines);
$in_comment=false;
for($i=0;$i<$linecount;$i++)
{
if(preg_match("/^/*/",preg_quote($lines[$i])))
{
$in_comment=true;
}
if(!$in_comment)
{
$output.=$lines[$i]." ";
}
if(preg_match("/*/$/",preg_quote($lines[$i])))
{
$in_comment=false;
}
}
unset($lines);
return$output;
}
//
//remove_
//
functionremove_remarks($sql)
{
$lines=explode(" ",$sql);
//trytokeepmem.usedown
$sql="";
$linecount=count($lines);
$output="";
for($i=0;$i<$linecount;$i++)
{
if(($i!=($linecount-1))||(strlen($lines[$i])>0))
{
if(isset($lines[$i][0])&&$lines[$i][0]!="#")
{
$output.=$lines[$i]." ";
}
else
{
$output.=" ";
}
//Tradingabitofspeedforlowermem.usehere.
$lines[$i]="";
}
}
return$output;
}
//
//split_sql_.
//Note:expectstrim()tohavealreadybeenrunon$sql.
//
functionsplit_sql_file($sql,$delimiter)
{
//Splitupourstringinto"possible"SQLstatements.
$tokens=explode($delimiter,$sql);
//trytosavemem.
$sql="";
$output=array();
//wedon'.
$matches=array();
//thisisfasterthancallingcount($oktens)everytimethrutheloop.
$token_count=count($tokens);
for($i=0;$i<$token_count;$i++)
{
//Don'.
if(($i!=($token_count-1))||(strlen($tokens[$i]>0)))
{
//.
$total_quotes=preg_match_all("/'/",$tokens[$i],$matches);
//,
//whichmeansthey'reescapedquotes.
$escaped_quotes=preg_match_all("/(?<!\\)(\\\\)*\\'/",$tokens[$i],$matches);
$unescaped_quotes=$total_quotes-$escaped_quotes;
//,.
if(($unescaped_quotes%2)==0)
{
//It'sacompletesqlstatement.
$output[]=$tokens[$i];
//savememory.
$tokens[$i]="";
}
else
{
//incompletesqlstatement..
//$tempwillholdwhatwehavesofar.
$temp=$tokens[$i].$delimiter;
//savememory..
$tokens[$i]="";
//Dowehaveacompletestatementyet?
$complete_stmt=false;
for($j=$i+1;(!$complete_stmt&&($j<$token_count));$j++)
{
//.
$total_quotes=preg_match_all("/'/",$tokens[$j],$matches);
//,
//whichmeansthey'reescapedquotes.
$escaped_quotes=preg_match_all("/(?<!\\)(\\\\)*\\'/",$tokens[$j],$matches);
$unescaped_quotes=$total_quotes-$escaped_quotes;
if(($unescaped_quotes%2)==1)
{
//oddnumberofunescapedquotes.
//statement(s),wenowhaveacompletestatement.(2oddsalwaysmakeaneven)
$output[]=$temp.$tokens[$j];
//savememory.
$tokens[$j]="";
$temp="";
//exittheloop.
$complete_stmt=true;
//.
$i=$j;
}
else
{
//evennumberofunescapedquotes.Westilldon'thaveacompletestatement.
//(1oddand1evenalwaysmakeanodd)
$temp.=$tokens[$j].$delimiter;
//savememory.
$tokens[$j]="";
}
}//for..
}//else
}
}
return$output;
}
$dbms_schema='yourfile.sql';
$sql_query=@fread(@fopen($dbms_schema,'r'),@filesize($dbms_schema))ordie('problem');
$sql_query=remove_remarks($sql_query);
$sql_query=split_sql_file($sql_query,';');
$host='localhost';
$user='user';
$pass='pass';
$db='database_name';
mysql_connect($host,$user,$pass)ordie('errorconnection');
mysql_select_db($db)ordie('errordatabaseselection');
$i=1;
foreach($sql_queryas$sql){
echo$i++;
echo"
";
mysql_query($sql)ordie('errorinquery');
}
?>
『柒』 PHP源碼運行顯示「執行SQL失敗:No database selected」
這個錯誤表示你沒有指定查詢語句的資料庫,你知道資料庫是什麼意思嗎,可以理解存放表的文件夾。
假設你的表tj在資料庫test裡面,那麼SQL語句:
select count(*) from tj ....
就應該修改為:
select count(*) from test.tj ....
也就是說在表名的前面添加資料庫名稱和小數點(英文),可以明確的指定資料庫。
還有一個方法,就是在mysql_connect連接到伺服器之後,使用下面的語句選擇資料庫test,表示以後所有的表沒有指定資料庫都是指test資料庫:
mysql_select_db('test');
『捌』 怎樣在PHP里執行SQL腳本
先看個例子吧:
<?php
/*配置項*/
$mysql_server_name='localhost';
$mysql_username='root'; //用戶名
$mysql_password='12345678'; //密碼
$mysql_database='mycounter'; //資料庫名
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //連接伺服器
$sql='CREATE DATABASE mycounter DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci; //sql語句創建資料庫
';
mysql_query($sql); //此處執行SQL語句
$sql='CREATE TABLE `counter` (`id` INT(255) UNSIGNED NOT NULL AUTO_INCREMENT ,`count` INT(255) UNSIGNED NOT NULL DEFAULT 0,PRIMARY KEY ( `id` ) ) TYPE = innodb;';
//sql語句創建表
mysql_select_db($mysql_database,$conn); //連接資料庫
$result=mysql_query($sql); //此處執行SQL語句
mysql_close($conn);
echo "Hello!資料庫mycounter已經成功建立!";
?>
所以說在PHP里執行SQL腳本就是利用mysql_query(『sql語句』)來執行的 (當然此處是只資料庫是MYSQL的情況下,如過是sqlserver則是mssql_query(『sql語句』))
『玖』 PHP執行sql返回不了結果
你的語句錯了 ,mysqli_fetch_array() 的參數必須是一個sql 結果集 , 而您給的是一個sql語句字元串。
就是說你缺少 sql 的過程
<?php
include('conn.php');
$phone=$_POST['phone'];
$check_query="select*frompersonswherephone='$phone'";
$result=$mysqli->query($check_query);//sql過程
if($row=$result->fetch_array(MYSQL_ASSOC)){
session_start();
$_SESSION['phone']=$row['phone'];
echo"<p>";
echo$phone,'成功';
}else{
exit('失敗!點擊此處<ahref="javascript:history.back(-1);">返回</a>重試');
}
?>