在我的存储过程中处理此错误的正确方法是什么?
我从 PHP 调用 SQL 存储过程。存储过程检查数据库,如果数据是新的,则插入数据,或者修改已存在的数据。
我收到一条错误消息,指出我的参数之一的数据类型不正确。由于数据来自何处,我无法真正控制它。我在数据数组中循环调用存储过程数百或数千次,并且仅针对批次中的几条记录出现此错误。
在存储过程中处理此错误的正确方法是什么?如果任何参数的数据类型不正确,我只想跳过该记录并转到下一条记录。每个调用都来自 foreach 循环。
数据库正在报告错误并且 PHP 正在显示它。我没有错误处理的经验。
PHP 代码示例:
foreach($item_array as $item) {
$id = $item['id'];
$color = $item['color'];
$con = connect()
$query = 'EXECUTE PROCEDURE sp_update_db(:id, :color);'
$params = array(':id' => $id, ':color' => $color);
$stmt = prepare($con, $query);
$result = execute($stmt, $params);
close($con);
}
运行代码时,我收到“警告:SQL 错误:[存储过程参数的数据类型不正确]”。
From PHP, I am calling a SQL stored procedure. The stored procedure checks the database, and either inserts the data if it's new, or modifies data that already exists.
I'm getting an error that one of my parameters is of an incorrect data type. I can't really control this because of where the data is coming from. I am calling the stored procedure hundreds, or thousands of times looping through an array of data, and I only get this error for a couple of records out of the batch.
What is the proper way to handle this error in the stored procedure? If any of the parameters have the incorrect data type, I just want to skip that record and move on to the next record. Each call comes from a foreach loop.
The database is reporting the error and PHP is displaying it. I do not have experience with error handling.
Example PHP Code:
foreach($item_array as $item) {
$id = $item['id'];
$color = $item['color'];
$con = connect()
$query = 'EXECUTE PROCEDURE sp_update_db(:id, :color);'
$params = array(':id' => $id, ':color' => $color);
$stmt = prepare($con, $query);
$result = execute($stmt, $params);
close($con);
}
Running the code I get "Warning: SQL error: [stored procedure a paramater was of the incorrect datatype]".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,由于传递了错误的参数,您的过程甚至没有被执行;你只能通过捕获异常来在你的 php 代码上处理这个问题。
如果实际上正在调用过程,但只是由于某种数据类型不匹配而导致过程内部失败,则可以使用
文档在这里。
更新
由于您说过您需要处理的每条记录都调用一次过程,因此您需要捕获PHP 端出现错误。您可以在 PHP 上使用
try/catch
块。 请参阅此处。基本上,您需要在
foreach
循环中包含try/catch
块,仅包含调用存储过程的部分; 但是,我只会在存储过程调用之前有一个if
语句,以确保将传递给过程的所有参数都具有预期的类型和长度。例如,如果存储过程需要int
类型的参数@a
,如果要传递的参数不是 a,我将完全阻止调用该过程。数字或空字符串。更新2
基于示例php(警告:我不是PHP编码员),似乎这会起作用:
但是同样,如果您知道过程期望的数据类型,为什么不使用
if
而不是try/catch
?Looks to me that your proc is not even executed as a result of the wrong parameter being passed; you can only handle this on your php code by catching the exception.
If the proc is in fact being called, but simply failling inside the procedure due to some sort of data type mismatch, you can use
Documentation here.
UPDATE
Since you said that you are calling the proc once per each record that you need to process, you need to catch the error on the PHP side. You can use
try/catch
blocks on PHP. See here.Basically, you'd need to have the
try/catch
block inside yourforeach
loop enclosing only the part that calls the stored procedure; however, I would just have anif
statement before the stored procedure call that makes sure all the parameters that will be passed to the proc are of the expected type and lenght. For example, if stored procedure expectes parameter@a
of typeint
, I would prevent the proc from being called at all if the parameter that is about to be passed is not a number or an empty string.UPDATE 2
Based on sample php (Warning: I am not a PHP coder), seems like this will work:
But again, if you know the data types expected by the proc, why not have an
if
instead of thetry/catch
?