警告:在多个 SQL 查询中使用 explode() 作为变量时,sqlsrv_fetch_array() 期望参数 1 为资源 | PHP、SQL
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource when using explode() as variable in multiple SQL query | PHP, SQL
为什么我会收到此错误消息?
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given.?
我在尝试INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
数据库时手动测试了它,具有以下值:$deptcode
:D001和$empno
:0000017,并且它完美运行。
这是 SQL 代码。
<?php
if(isset($_POST['referenceno']))
$data=$_POST['referenceno'];
$explode= explode(":", $data);
$user = $user['fingerscanno'];
$deptcode=$explode[0];
$emp=$explode[1];
$empno = str_pad(++$emp,7,"0",STR_PAD_LEFT);
$jobposition=$explode[2];
$departmentname=$explode[3];
$sql = "SELECT DISTINCT
LEFT (departmentcode,4) as deptcode,
MAX(RIGHT(employeeidno, 7)) as empno,
section, departmentname, paymentmode,
hrdjobpositionentry.jobposition as jobposition
FROM departmentmasterfile, hrdjobpositionentry,employeemasterfile
WHERE hrdjobpositionentry.department=departmentmasterfile.departmentname AND
LEFT (employeemasterfile.employeeidno,4) = LEFT (departmentmasterfile.departmentcode,4) AND
LEFT (departmentcode,4) = '$deptcode' AND hrdjobpositionentry.jobposition='$jobposition'
GROUP BY LEFT (departmentcode,4), section, departmentname, paymentmode, hrdjobpositionentry.jobposition
ORDER BY LEFT (departmentcode,4);
INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
while()
子句应该不是问题,对吗?因为我只在第二个 SQL 查询中插入。
此 SQL 查询仅在我删除 INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
时有效
编辑:这是 $conn
,这是通过主页中的 include()
加载的。
<?php
$serverName = "XX\XXXX";
$connectionInfo = array( "Database"=>"XXXX", "UID"=>"XX", "PWD"=>"XXXX",
'ReturnDatesAsStrings'=>true,"CharacterSet" => "UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
说明:
你的错误原因是sqlsrv_query()
returnsfalse
,也就是说你的语句没有正确执行:
Return value (sqlsrv_query):
A statement resource. If the statement cannot be created and/or
executed, false is returned.
如果语句没有正确执行,您需要检查sqlsrv_query()
的结果并得到sqlsrv_errors()
的错误。
<?php
...
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
...
?>
补充说明:
尝试使用 parameterized 查询。函数 sqlsrv_query()
支持使用参数:
The sqlsrv_query function is well-suited for one-time queries and
should be the default choice to execute queries unless special
circumstances apply. This function provides a streamlined method to
execute a query with a minimum amount of code. The sqlsrv_query
function does both statement preparation and statement execution, and
can be used to execute parameterized queries.
示例(基于您的代码):
<?php
if (!isset($_POST['referenceno'])) {
echo "Error."
exit;
}
$data = $_POST['referenceno'];
//$user = $user['fingerscanno'];
$explode = explode(":", $data);
$deptcode = $explode[0];
$emp = $explode[1];
$empno = str_pad(++$emp, 7, "0", STR_PAD_LEFT);
$jobposition = $explode[2];
$departmentname = $explode[3];
$sql = "
SELECT
DISTINCT LEFT (departmentcode,4) as deptcode,
MAX(RIGHT(employeeidno, 7)) as empno,
section,
departmentname,
paymentmode,
hrdjobpositionentry.jobposition as jobposition
FROM departmentmasterfile, hrdjobpositionentry,employeemasterfile
WHERE
hrdjobpositionentry.department = departmentmasterfile.departmentname AND
LEFT (employeemasterfile.employeeidno,4) = LEFT (departmentmasterfile.departmentcode,4) AND
LEFT (departmentcode,4) = ? AND hrdjobpositionentry.jobposition = ?
GROUP BY LEFT (departmentcode,4), section, departmentname, paymentmode, hrdjobpositionentry.jobposition
ORDER BY LEFT (departmentcode,4);
INSERT INTO employeemasterfile (employeeidno) VALUES (? + ?);
";
$params = array(
$deptcode,
$jobposition,
$deptcode,
$empno
);
$query = sqlsrv_query($conn, $sql, $params, array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
var_dump($row);
echo "<br>";
}
sqlsrv_free_stmt($query);
?>
为什么我会收到此错误消息?
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given.?
我在尝试INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
数据库时手动测试了它,具有以下值:$deptcode
:D001和$empno
:0000017,并且它完美运行。
这是 SQL 代码。
<?php
if(isset($_POST['referenceno']))
$data=$_POST['referenceno'];
$explode= explode(":", $data);
$user = $user['fingerscanno'];
$deptcode=$explode[0];
$emp=$explode[1];
$empno = str_pad(++$emp,7,"0",STR_PAD_LEFT);
$jobposition=$explode[2];
$departmentname=$explode[3];
$sql = "SELECT DISTINCT
LEFT (departmentcode,4) as deptcode,
MAX(RIGHT(employeeidno, 7)) as empno,
section, departmentname, paymentmode,
hrdjobpositionentry.jobposition as jobposition
FROM departmentmasterfile, hrdjobpositionentry,employeemasterfile
WHERE hrdjobpositionentry.department=departmentmasterfile.departmentname AND
LEFT (employeemasterfile.employeeidno,4) = LEFT (departmentmasterfile.departmentcode,4) AND
LEFT (departmentcode,4) = '$deptcode' AND hrdjobpositionentry.jobposition='$jobposition'
GROUP BY LEFT (departmentcode,4), section, departmentname, paymentmode, hrdjobpositionentry.jobposition
ORDER BY LEFT (departmentcode,4);
INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
";
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)){
while()
子句应该不是问题,对吗?因为我只在第二个 SQL 查询中插入。
此 SQL 查询仅在我删除 INSERT INTO employeemasterfile (employeeidno) VALUES ('$deptcode'+'$empno');
编辑:这是 $conn
,这是通过主页中的 include()
加载的。
<?php
$serverName = "XX\XXXX";
$connectionInfo = array( "Database"=>"XXXX", "UID"=>"XX", "PWD"=>"XXXX",
'ReturnDatesAsStrings'=>true,"CharacterSet" => "UTF-8");
$conn = sqlsrv_connect( $serverName, $connectionInfo);
if( $conn ) {
}else{
echo "Connection could not be established.<br />";
die( print_r( sqlsrv_errors(), true));
}
?>
说明:
你的错误原因是sqlsrv_query()
returnsfalse
,也就是说你的语句没有正确执行:
Return value (sqlsrv_query):
A statement resource. If the statement cannot be created and/or executed, false is returned.
如果语句没有正确执行,您需要检查sqlsrv_query()
的结果并得到sqlsrv_errors()
的错误。
<?php
...
$query = sqlsrv_query($conn, $sql, array(), array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
...
?>
补充说明:
尝试使用 parameterized 查询。函数 sqlsrv_query()
支持使用参数:
The sqlsrv_query function is well-suited for one-time queries and should be the default choice to execute queries unless special circumstances apply. This function provides a streamlined method to execute a query with a minimum amount of code. The sqlsrv_query function does both statement preparation and statement execution, and can be used to execute parameterized queries.
示例(基于您的代码):
<?php
if (!isset($_POST['referenceno'])) {
echo "Error."
exit;
}
$data = $_POST['referenceno'];
//$user = $user['fingerscanno'];
$explode = explode(":", $data);
$deptcode = $explode[0];
$emp = $explode[1];
$empno = str_pad(++$emp, 7, "0", STR_PAD_LEFT);
$jobposition = $explode[2];
$departmentname = $explode[3];
$sql = "
SELECT
DISTINCT LEFT (departmentcode,4) as deptcode,
MAX(RIGHT(employeeidno, 7)) as empno,
section,
departmentname,
paymentmode,
hrdjobpositionentry.jobposition as jobposition
FROM departmentmasterfile, hrdjobpositionentry,employeemasterfile
WHERE
hrdjobpositionentry.department = departmentmasterfile.departmentname AND
LEFT (employeemasterfile.employeeidno,4) = LEFT (departmentmasterfile.departmentcode,4) AND
LEFT (departmentcode,4) = ? AND hrdjobpositionentry.jobposition = ?
GROUP BY LEFT (departmentcode,4), section, departmentname, paymentmode, hrdjobpositionentry.jobposition
ORDER BY LEFT (departmentcode,4);
INSERT INTO employeemasterfile (employeeidno) VALUES (? + ?);
";
$params = array(
$deptcode,
$jobposition,
$deptcode,
$empno
);
$query = sqlsrv_query($conn, $sql, $params, array("Scrollable" => SQLSRV_CURSOR_KEYSET));
if ($query === false) {
echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
exit;
}
while($row = sqlsrv_fetch_array($query, SQLSRV_FETCH_ASSOC)) {
var_dump($row);
echo "<br>";
}
sqlsrv_free_stmt($query);
?>