‘壹’ 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>重试');
}
?>