我刚刚开始学习PHP,我很难将mysql_result转换为使用sqlsrv的东西。
我想要转换的代码是: (编辑为包含完整代码)
function database($querydb) {
global $global;
global $field;
if (isset($global['queries'])) {
$global['queries']++;
} else {
$global['queries'] = "1";
}
$field['queries'] = $global['queries'];
if (isset($global['query_log'])) {
$global['query_log'] .= "\n<br>$querydb";
} else {
$global['query_log'] = "$querydb";
}
$serverName = "XXX";
$uid = "XXX";
$pwd = "XXX";
$dbName = "XXX";
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => $dbName, "ReturnDatesAsStrings"=>true);
$conn = sqlsrv_connect($serverName, $connectionInfo);
$stmt = sqlsrv_query($conn, $querydb) or return_error("Query Error: $querydb");
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
$global['dbresult'] = $row;
}
if ((substr($querydb,0,6)!="INSERT") && (substr($querydb,0,6)!="UPDATE") && (substr($querydb,0,6)!="DELETE")) {
while ($row1 = sqlsrv_fetch_array($stmt))
{
$global['dbnumber'] = mysql_numrows($global['dbresult']); // original $dbnumber
}
return;
}
function return_error($error) {
print $error;
exit;
}
function date_status($date, $username) {
global $global;
global $field;
global $input;
global $text;
$status = "0";
if ($username!="") {
$query = "SELECT countedrow.total, id, start_date, end_date FROM calendar JOIN (SELECT total = COUNT(*) FROM oc_calendar) AS countedrow ON 1=1";
database($query);
for ($i = 0; $i < $global['dbnumber']; $i++) {
$status = "1";
$event_id = sqlsrv_fetch_array($global['dbresult'],$i,"id");
}
}
return $status;
}
我试过sqlsrv_get_field,sqlsrv_fetch,sqlsrv_fetch_array,但我显然没有正确的语法,并且因为我收到错误而误解了这个:
sqlsrv_fetch_array()期望参数1是资源,在...中给出的数组
......无论我做什么。
如何从数组中提取id以设置$ event_id?我哪里错了?
答案 0 :(得分:0)
$querydb = "SELECT countedrow.total, id, start_date, end_date FROM oc_calendar JOIN (SELECT total = COUNT(*) FROM oc_calendar) AS countedrow ON 1=1";
$stmt = sqlsrv_query($conn, $querydb);
if( $stmt === false) {
die( print_r( sqlsrv_errors(), true) );
}
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
$event_id = $row["id"];
}
这有用吗?
为什么你在一段时间内将所有行分别映射到globalvar,然后再次循环遍历全局变量!如果你需要将它从函数返回全局,然后将其作为返回数组传递而不是全局
,则直接将它全部循环答案 1 :(得分:0)
您说您不知道如何为sqlsrv_fetch_array()
提供有效的第一个参数,但您的代码已正确执行:
$stmt = sqlsrv_query($conn, $querydb) or return_error("Query Error: $querydb");
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) ) {
}
然而你迷失在你自己的意大利面中:
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_NUMERIC) )
{
$global['dbresult'] = $row;
}
$event_id = sqlsrv_fetch_array($global['dbresult'],$i,"id");
老实说,我建议您删除此代码并从头开始。