Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP、MSSQL、SQLSRV(已编写)驱动程序;如何获取行I';你刚插进去?_Php_Sql Server_Prepared Statement - Fatal编程技术网

PHP、MSSQL、SQLSRV(已编写)驱动程序;如何获取行I';你刚插进去?

PHP、MSSQL、SQLSRV(已编写)驱动程序;如何获取行I';你刚插进去?,php,sql-server,prepared-statement,Php,Sql Server,Prepared Statement,我希望使用SQLSRV生成最后一个插入的id。不过我需要使用一个准备好的语句。我在这里看到了一个答案(请参阅下面代码后面的链接),说明了如何使用它,但该语句不是为反sql注入而准备的 //Prep the variables for insert $p1 = $_POST['description']; $p2 = intval($_POST['visible']); $p3 = strval($_POST['whoToShow']); //Build an array with those

我希望使用SQLSRV生成最后一个插入的id。不过我需要使用一个准备好的语句。我在这里看到了一个答案(请参阅下面代码后面的链接),说明了如何使用它,但该语句不是为反sql注入而准备的

//Prep the variables for insert
$p1 = $_POST['description'];
$p2 = intval($_POST['visible']);
$p3 = strval($_POST['whoToShow']);

//Build an array with those variables
$params = array(&$p1, &$p2, &$p3);

//Build the SQL
$sql = "INSERT INTO notifications (description, visible, whoToShow) VALUES (?, ?, ?)";

//Execute the sql using a prepared statement, passing the variables in an array
$stmt = sqlsrv_prepare($conn, $sql, $params) or die(FormatErrors(sqlsrv_errors())); 
有关使用非准备语句获取最后插入的ID的详细信息,请查看堆栈溢出


提前感谢您的支持。

请考虑使用存储过程而不是直接插入语句。使用存储过程更好,因为可以从存储过程返回一个记录集,其中包括插入记录的ID

我在PHP中使用Microsoft SQL Server。我正在使用mssql_查询库连接到SQL server。 不确定这是否有区别,但我看到您正在使用不同的库进行连接。我们所做的每个查询都是通过存储过程进行的。它的效率更高,而且绝对更安全

$myServer = "xxxxxxx";
$myUser = "xxxxxxxx";
$myPass = "xxxxxxx";
$myDB = "myDatabase";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");

$query = "exec eCommerce.dbo.cart_GET_Detail    @sid = ".$_SESSION['sid']." , @cc = '".$_SESSION['cc']."'";

$result = mssql_query($query);
$numRows = mssql_num_rows($result);
$hasItems = (($numRows == 0) ? 'N' : 'Y');

while ($RSLoop = mssql_fetch_array($result)) {
    //var_dump($RSLoop);  //var_dump will show you everything in the recordset
    echo '<tr><td colspan=6 width=720 class=cartDivider>&nbsp;</td></tr>';
    echo '<form name=frmProduct'.$idx.' method=POST action=_action.asp>';
    echo '<input type=hidden name=pid value="'.$RSLoop['product_id'].'">';
}
$myServer=“xxxxxxx”;
$myUser=“xxxxxxxx”;
$myPass=“xxxxxxx”;
$myDB=“myDatabase”;
//与数据库的连接
$dbhandle=mssql\u connect($myServer、$myUser、$myPass)
或者死亡(“无法连接到$myServer上的SQL Server”);
//选择要使用的数据库
$selected=mssql\u select\u db($myDB,$dbhandle)
或者死亡(“无法打开数据库$myDB”);
$query=“exec eCommerce.dbo.cart_GET_Detail@sid=”.$_SESSION['sid'.”,@cc=“$_SESSION['cc'.]”;
$result=mssql_查询($query);
$numRows=mssql\u num\u行($result);
$hasItems=($numRows==0)?'N':'Y');
而($RSLoop=mssql\u fetch\u数组($result)){
//var_dump($RSLoop);//var_dump将显示记录集中的所有内容
回声';
回声';
回声';
}
这是对存储过程的调用,以获取存储在SQL表中的购物车内容。
在存储过程上执行插入类似。您应该能够在SQL Server存储过程中找到一些代码示例。

您是否有当前正在使用的存储过程,我可以查看一下?很高兴看到它是如何集成到php脚本中的。我添加了一些代码来调用存储过程。有关编写自己的存储过程的示例,请参阅本页: