【发布时间】:2018-01-18 20:13:19
【问题描述】:
当我在 return 语句之前转储数组时,一切似乎都很好。 但是,当我转储结果时,它似乎是空的。
应该更清楚包含的代码。我从 DatabaseHandler 调用 GetRow,当我在返回之前进行转储时,我可以看到有一个数组。 (参见 var_dump)
public static function GetRow($sqlQuery, $params = null,
$fetchStyle = PDO::FETCH_ASSOC)
{
// Initialize the return value to null
$result = null;
// Try to execute an SQL query or a stored procedure
try
{
// Get the database handler
$database_handler = self::GetHandler();
// Prepare the query for execution
$statement_handler = $database_handler->prepare($sqlQuery);
// Execute the query
$statement_handler->execute($params);
// Fetch result
$result = $statement_handler->fetch($fetchStyle);
}
// Trigger an error if an exception was thrown when executing the SQL query
catch(PDOException $e)
{
// Close the database handler and trigger an error
self::Close();
trigger_error($e->getMessage(), E_USER_ERROR);
}
// Return the query results
exit(var_dump($result)); // SHOWS A PROPER ARRAY
return $result;
}
在调用它的另一个函数中,什么都看不到(假):
// Gets the details of a specific order
public static function GetOrderInfo($orderId)
{
// Build the SQL query
$sql = 'CALL orders_get_order_info(:order_id)';
// Build the parameters array
$params = array (':order_id' => $orderId);
// Execute the query and return the results
$result = DatabaseHandler::GetRow($sql, $params);
exit(var_dump($result)); // SHOWS FALSE
return $result
}
【问题讨论】:
-
不要
exit它永远不要returns... -
在 var_dump 后使用 return 并移除 exit。
-
这应该被标记为拼写错误
-
exit(var_dump($result));将显示var_dump(...)的返回值(as shown in the documentation 什么都不是)。它不会向您显示与var_dump($result); exit();相同的内容。 -
谢谢,但是转储只是为了显示变量中的内容。如果我删除所有退出语句, $result 只是错误