将 MSSQL 脚本转换为 PHP SQLSRV 查询无效
Converted MSSQL script to PHP SQLSRV query not working
我相信比我知识渊博的人可以提供帮助...
我有一个查询,将商店 sql 数据库中的产品与网站 mysql 数据库中的产品进行比较并更新它。
它必须从 php mysql 转换为 sqlsrv,因为我的主机停止支持它。
我尝试转换,但不幸的是我破坏了查询,它已停止更新网站 mysql 数据库。
任何人都可以帮助我解决问题或看到任何明显的错误吗?理想情况下,我想看看它在哪里失败,因为 sql 数据库中的数据是正确的,但没有在 mysql 数据库中更新。
感谢任何帮助:-)
原代码使用phpmysql和mysql
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// mssql_query() actually returns a resource
// that you must iterate with (e.g.) mssql_fetch_array()
$mssqlResult = mssql_query($query);
if($mssqlResult === FALSE) {
die(mysql_error()); // TODO: better error handling
}
// connect to the MySQL database
mysql_connect("XXXXXX", "XXXXXX", "XXXXXX") or die(mysql_error());
mysql_select_db("XXXXXX") or die(mysql_error());
while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
mysql_query(
"UPDATE product_option_relation SET stock = $mssqlOnHand WHERE sku = '$mssqlCode'"
// extra quotes may be required around $mssqlCode depending on the column type
);
mysql_query(
"UPDATE product SET quantity = $mssqlOnHand WHERE sku = '$mssqlCode' AND has_option = '0' ");
}
//close the connection
mssql_close($dbhandle);
// Update product total with the total quantities of the product options
$result = mysql_query("UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty")
or die(mysql_error());
?>
使用 sqlsrv 和 mysqli
的新代码
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//Connection to the database
$connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);
@$conn = sqlsrv_connect( $Server, $connectionInfo);
if( $conn === false) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// sqlsrv_query() actually returns a resource
// that you must iterate with (e.g.) sqlsrv_fetch_array()
@$mssqlResult = sqlsrv_query( $conn, $sql);
if( $mssqlResult === false) {
echo "No result resource after MSSQL query.<br />";
die( print_r( sqlsrv_errors(), true) );
}
// connect to the MySQL database
@$db = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "XXXXXX");
if (mysqli_connect_error()) {
echo "Error: Unable to connect to MySQL.<br />";
exit;
}
while( $mssqlRow = sqlsrv_fetch_array( $mssqlResult, SQLSRV_FETCH_ASSOC) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
$mysqlQuery = "UPDATE product_option_relation SET stock = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."'";
mysqli_query($db, $mysqlQuery);
$mysqlQuery = "UPDATE product SET quantity = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."' AND has_option = '0' ";
mysqli_query($db, $mysqlQuery);
}
//close the MSSRV connections
sqlsrv_free_stmt($mssqlResult);
sqlsrv_close($conn);
// Update product total with the total quantities of the product options
$mysqlQuery = "UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty";
@$result = mysqli_query($db, $mysqlQuery);
if(!$result){
echo "No result resource after MySQL query.<br />";
}
//Close MySQL connection
//mysqli_free_result($result);
mysqli_close($db);
?>
谢谢 Chris,@ 符号有几个拼写错误,所以我删除了它们 - 我通过更改行
设法解决了这个问题
@$mssqlResult = sqlsrv_query( $conn, $sql);
阅读
$mssqlResult = sqlsrv_query( $conn, $query);
变量应该读取 $query 而不是 $sql。感谢您帮助故障排除伙伴。所以完整的查询读取
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//Connection to the database
$connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);
$conn = sqlsrv_connect( $Server, $connectionInfo);
if( $conn === false) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// sqlsrv_query() actually returns a resource
// that you must iterate with (e.g.) sqlsrv_fetch_array()
$mssqlResult = sqlsrv_query( $conn, $query);
if( $mssqlResult === false) {
echo "No result resource after MSSQL query.<br />";
die( print_r( sqlsrv_errors(), true) );
}
// connect to the MySQL database
$db = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "XXXXXX");
if (mysqli_connect_error()) {
echo "Error: Unable to connect to MySQL.<br />";
exit;
}
while( $mssqlRow = sqlsrv_fetch_array( $mssqlResult, SQLSRV_FETCH_ASSOC) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
$mysqlQuery = "UPDATE product_option_relation SET stock = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."'";
mysqli_query($db, $mysqlQuery);
$mysqlQuery = "UPDATE product SET quantity = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."' AND has_option = '0' ";
mysqli_query($db, $mysqlQuery);
}
//close the MSSRV connections
sqlsrv_free_stmt($mssqlResult);
sqlsrv_close($conn);
// Update product total with the total quantities of the product options
$mysqlQuery = "UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty";
$result = mysqli_query($db, $mysqlQuery);
if(!$result){
echo "No result resource after MySQL query.<br />";
}
//Close MySQL connection
//mysqli_free_result($result);
mysqli_close($db);
?>
我相信比我知识渊博的人可以提供帮助...
我有一个查询,将商店 sql 数据库中的产品与网站 mysql 数据库中的产品进行比较并更新它。
它必须从 php mysql 转换为 sqlsrv,因为我的主机停止支持它。
我尝试转换,但不幸的是我破坏了查询,它已停止更新网站 mysql 数据库。
任何人都可以帮助我解决问题或看到任何明显的错误吗?理想情况下,我想看看它在哪里失败,因为 sql 数据库中的数据是正确的,但没有在 mysql 数据库中更新。
感谢任何帮助:-)
原代码使用phpmysql和mysql
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//connection to the database
$dbhandle = mssql_connect($Server, $User, $Pass)
or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
//select a database to work with
$selected = mssql_select_db($DB, $dbhandle)
or die("Couldn't connect to SQL Server on $Server. Error: " . mssql_get_last_message());
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// mssql_query() actually returns a resource
// that you must iterate with (e.g.) mssql_fetch_array()
$mssqlResult = mssql_query($query);
if($mssqlResult === FALSE) {
die(mysql_error()); // TODO: better error handling
}
// connect to the MySQL database
mysql_connect("XXXXXX", "XXXXXX", "XXXXXX") or die(mysql_error());
mysql_select_db("XXXXXX") or die(mysql_error());
while ( $mssqlRow = mssql_fetch_array($mssqlResult) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
mysql_query(
"UPDATE product_option_relation SET stock = $mssqlOnHand WHERE sku = '$mssqlCode'"
// extra quotes may be required around $mssqlCode depending on the column type
);
mysql_query(
"UPDATE product SET quantity = $mssqlOnHand WHERE sku = '$mssqlCode' AND has_option = '0' ");
}
//close the connection
mssql_close($dbhandle);
// Update product total with the total quantities of the product options
$result = mysql_query("UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty")
or die(mysql_error());
?>
使用 sqlsrv 和 mysqli
的新代码<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//Connection to the database
$connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);
@$conn = sqlsrv_connect( $Server, $connectionInfo);
if( $conn === false) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// sqlsrv_query() actually returns a resource
// that you must iterate with (e.g.) sqlsrv_fetch_array()
@$mssqlResult = sqlsrv_query( $conn, $sql);
if( $mssqlResult === false) {
echo "No result resource after MSSQL query.<br />";
die( print_r( sqlsrv_errors(), true) );
}
// connect to the MySQL database
@$db = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "XXXXXX");
if (mysqli_connect_error()) {
echo "Error: Unable to connect to MySQL.<br />";
exit;
}
while( $mssqlRow = sqlsrv_fetch_array( $mssqlResult, SQLSRV_FETCH_ASSOC) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
$mysqlQuery = "UPDATE product_option_relation SET stock = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."'";
mysqli_query($db, $mysqlQuery);
$mysqlQuery = "UPDATE product SET quantity = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."' AND has_option = '0' ";
mysqli_query($db, $mysqlQuery);
}
//close the MSSRV connections
sqlsrv_free_stmt($mssqlResult);
sqlsrv_close($conn);
// Update product total with the total quantities of the product options
$mysqlQuery = "UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty";
@$result = mysqli_query($db, $mysqlQuery);
if(!$result){
echo "No result resource after MySQL query.<br />";
}
//Close MySQL connection
//mysqli_free_result($result);
mysqli_close($db);
?>
谢谢 Chris,@ 符号有几个拼写错误,所以我删除了它们 - 我通过更改行
设法解决了这个问题@$mssqlResult = sqlsrv_query( $conn, $sql);
阅读
$mssqlResult = sqlsrv_query( $conn, $query);
变量应该读取 $query 而不是 $sql。感谢您帮助故障排除伙伴。所以完整的查询读取
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$Server = "XXXXXX";
$User = "XXXXXX";
$Pass = "XXXXXX";
$DB = "XXXXXX";
//Connection to the database
$connectionInfo = array( "Database"=>$DB, "UID"=>$User, "PWD"=>$Pass);
$conn = sqlsrv_connect( $Server, $connectionInfo);
if( $conn === false) {
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
//declare the SQL statement that will query the database
$query = "select I.ID, I.Code Code, H.OnHand / coalesce(StU.UnitSize, 1) OnHand \n";
$query .= "from Products I \n";
$query .= "left join ProductStockLocations St on (St.ProductID = I.ID) \n";
$query .= "left join ProductPOSLocations L on (L.ProductID = I.ID) \n";
$query .= "left join ProductUnits StU on (StU.ID = St.StockUnitID) \n";
$query .= "left join ProductStockOnHandItems H on (H.ProductID = I.ID and H.StockLocationID = St.StockLocationID) \n";
$query .= "where I.ID > 0 \n";
$query .= "and St.StockLocationID = 1 \n";
$query .= "and L.POSLocationID = 1 \n";
$query .= "and L.SoldAtLocation = 1 \n";
// execute the SQL query and return a result set
// sqlsrv_query() actually returns a resource
// that you must iterate with (e.g.) sqlsrv_fetch_array()
$mssqlResult = sqlsrv_query( $conn, $query);
if( $mssqlResult === false) {
echo "No result resource after MSSQL query.<br />";
die( print_r( sqlsrv_errors(), true) );
}
// connect to the MySQL database
$db = mysqli_connect("XXXXXX", "XXXXXX", "XXXXXX", "XXXXXX");
if (mysqli_connect_error()) {
echo "Error: Unable to connect to MySQL.<br />";
exit;
}
while( $mssqlRow = sqlsrv_fetch_array( $mssqlResult, SQLSRV_FETCH_ASSOC) ) {
$mssqlCode = $mssqlRow['Code'];
$mssqlOnHand = $mssqlRow['OnHand'];
$mysqlQuery = "UPDATE product_option_relation SET stock = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."'";
mysqli_query($db, $mysqlQuery);
$mysqlQuery = "UPDATE product SET quantity = ".$mssqlOnHand." WHERE sku = '".$mssqlCode."' AND has_option = '0' ";
mysqli_query($db, $mysqlQuery);
}
//close the MSSRV connections
sqlsrv_free_stmt($mssqlResult);
sqlsrv_close($conn);
// Update product total with the total quantities of the product options
$mysqlQuery = "UPDATE product a INNER JOIN (SELECT product_id, SUM(stock) AS summedqty FROM product_option_relation GROUP BY product_id) b ON b.product_id = a.product_ID SET a.quantity = summedqty";
$result = mysqli_query($db, $mysqlQuery);
if(!$result){
echo "No result resource after MySQL query.<br />";
}
//Close MySQL connection
//mysqli_free_result($result);
mysqli_close($db);
?>