PHP、XAMPP、SQL SERVER 2012 中 array_push() 的等效 sqlsrv_
Equivalent sqlsrv_ for array_push() in PHP, XAMPP, SQL SERVER 2012
在sqlsrv_
中是否有array_push()
的等效代码?喜欢sqlsrv_array_push()
。我不确定它是否有,我还没有阅读有关该代码的文档。有替代方案吗?
我试着重新编码,这是我目前所得到的。
编辑:添加了 conn.php
<?php
$serverName = "XXXXXX\XXXXXX";
$connectionInfo = array( "Database"=>"XXXXXX", "UID"=>"XXXXXX", "PWD"=>"XXXXXX");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
编辑:添加了 $and
$and = 'AND YEAR(date) = '.$year;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'I' $and";
$oquery = $conn->query($sql);
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'O' $and";
$lquery = $conn->query($sql);
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
}
这是我遇到的错误。
Warning: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\path\tofile\home.php on line 198
说明:
如果我理解你的逻辑,你需要将 $conn->query($sql);
替换为 sqlsrv_query() 调用。函数 sqlsrv_?
是 SQL 服务器的 PHP 驱动程序的一部分,并且不是面向对象的。
示例:
下一个示例基于您的代码,可能有助于获得您的预期结果:
<?php
$and = 'AND YEAR(CHECKTIME) = '.$year;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'I' $and";
$oquery = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($oquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'O' $and";
$lquery = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($lquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
?>
备注:
您也可以考虑使用参数化查询:
<?php
$y = 2019;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE YEAR(CHECKTIME) = ? AND MONTH(CHECKTIME) = ? AND CHECKTYPE = 'I'";
$oquery = sqlsrv_query($conn, $sql, array(&$y, &$m), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($oquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE YEAR(CHECKTIME) = ? AND MONTH(CHECKTIME) = ? AND CHECKTYPE = 'O'";
$lquery = sqlsrv_query($conn, $sql, array(&$y, &$m), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($lquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
}
?>
在sqlsrv_
中是否有array_push()
的等效代码?喜欢sqlsrv_array_push()
。我不确定它是否有,我还没有阅读有关该代码的文档。有替代方案吗?
我试着重新编码,这是我目前所得到的。
编辑:添加了 conn.php
<?php
$serverName = "XXXXXX\XXXXXX";
$connectionInfo = array( "Database"=>"XXXXXX", "UID"=>"XXXXXX", "PWD"=>"XXXXXX");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
echo "Connection established.<br />";
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
编辑:添加了 $and
$and = 'AND YEAR(date) = '.$year;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'I' $and";
$oquery = $conn->query($sql);
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'O' $and";
$lquery = $conn->query($sql);
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
}
这是我遇到的错误。
Warning: sqlsrv_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\path\tofile\home.php on line 198
说明:
如果我理解你的逻辑,你需要将 $conn->query($sql);
替换为 sqlsrv_query() 调用。函数 sqlsrv_?
是 SQL 服务器的 PHP 驱动程序的一部分,并且不是面向对象的。
示例:
下一个示例基于您的代码,可能有助于获得您的预期结果:
<?php
$and = 'AND YEAR(CHECKTIME) = '.$year;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'I' $and";
$oquery = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($oquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE MONTH(CHECKTIME) = '$m' AND CHECKTYPE = 'O' $and";
$lquery = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($lquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
?>
备注:
您也可以考虑使用参数化查询:
<?php
$y = 2019;
$months = array();
$ontime = array();
$late = array();
for( $m = 1; $m <= 12; $m++ ) {
$sql = "SELECT * FROM CHECKINOUT WHERE YEAR(CHECKTIME) = ? AND MONTH(CHECKTIME) = ? AND CHECKTYPE = 'I'";
$oquery = sqlsrv_query($conn, $sql, array(&$y, &$m), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($oquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($ontime, sqlsrv_num_rows($oquery));
$sql = "SELECT * FROM CHECKINOUT WHERE YEAR(CHECKTIME) = ? AND MONTH(CHECKTIME) = ? AND CHECKTYPE = 'O'";
$lquery = sqlsrv_query($conn, $sql, array(&$y, &$m), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($lquery === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
array_push($late, sqlsrv_num_rows($lquery));
$num = str_pad( $m, 2, 0, STR_PAD_LEFT );
$month = date('M', mktime(0, 0, 0, $m, 1));
array_push($months, $month);
}
?>