[英]Can anyone tell me why isn't the query executing in the following php code?
[英]Can anyone tell me if they see an error in the following PHP code?
出於某種原因,下面的while循環永遠不會觸發。 所有這些都在課堂上。
$code = $this->get_postal_state_no('Western Cape');
private function get_postal_state_no($psn)
{
$sql = "
SELECT
no
FROM
ct_state
WHERE
name LIKE('".$psn."');";
$stmt = sqlsrv_query($this->conn1, $sql);
// This statement is not false, so the error handling does not happen, this is expected.
if($stmt === FALSE)
{
if(($errors = sqlsrv_errors()) != NULL)
{
foreach($errors as $error)
{
$sqlstate = $error['SQLSTATE'];
$code = $error['code'];
$sqlmessage = $error['message'];
}
}
$msg = 'Error in $stmt in get_postal_state_no() method.';
$this->do_error_log($error_msg, $sqlstate, $code, $sqlmessage, $msg, __LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__);
sqlsrv_free_stmt($stmt);
}
//This loop is never entered. This is not expected.
while($obj = sqlsrv_fetch_object($stmt))
{
echo "I am here now";
break;
if(!empty($obj->no) && $obj->no != '')
{
echo "Hello, I exist";
break;
// This break never happens
return $obj->no;
}
else
{
echo "Hello, I don't exist";
break;
// This break never happens
$code = $this->sp_aa_iud_ct_state($psn);
return $code;
}
sqlsrv_free_stmt($stmt);
}
}
有誰知道為什么? 我正在使用php_sqlserv驅動程序。 SQL Server Profiler shopws查詢第一部分執行。
謝謝
Ĵ
查看sqlsrv_fetch_object的文檔頁面,它說明了返回值:
成功時返回一個對象,如果沒有要返回的行,則返回NULL;如果發生錯誤或指定的類不存在,則返回FALSE。
我懷疑該函數要么返回NULL
,要么返回FALSE
,盡管原因有點不清楚。 嘗試在while
循環上面添加它,看看輸出是什么:
$obj = sqlsrv_fetch_object($stmt)
var_dump($obj);
die;
while($obj = sqlsrv_fetch_object($stmt))
{ // ...
另一個有用的調試技巧是轉儲查詢,然后將其復制/粘貼到您的DBMS系統中,並直接針對您的數據庫運行您在代碼中執行的查詢,以查看是否實際返回了任何結果:
$sql = "
SELECT
no
FROM
ct_state
WHERE
name LIKE('".$psn."');";
var_dump($sql);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.