array_push()
中的sqlsrv_
是否有等效代码?像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);
}
这是我得到的错误。
警告:sqlsrv_num_rows()希望参数1是资源,在198行的C:\ xampp \ htdocs \ path \ tofile \ home.php中给出布尔值
答案 0 :(得分:1)
说明:
如果我理解您的逻辑,则需要将$conn->query($sql);
替换为sqlsrv_query()调用。函数sqlsrv_?
是SQL Server 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);
}
?>