Php 试着用一个准备好的陈述来抓住
我在这个问题上经历了一段非常艰难的时光,有人建议我学习如何尝试捕捉块,以便更容易地找出错误所在。这是我第一次尝试 我的@stmt2未为我的print\r$stmt2行定义,因此出现错误 这是我的尝试。我有没有因为这个错误而做错什么Php 试着用一个准备好的陈述来抓住,php,try-catch,prepared-statement,Php,Try Catch,Prepared Statement,我在这个问题上经历了一段非常艰难的时光,有人建议我学习如何尝试捕捉块,以便更容易地找出错误所在。这是我第一次尝试 我的@stmt2未为我的print\r$stmt2行定义,因此出现错误 这是我的尝试。我有没有因为这个错误而做错什么 try { $con = mysqli_connect("localhost", "", "", ""); if (mysqli_connect_errno()) { throw new Exception("Connect failed: %s\n", my
try {
$con = mysqli_connect("localhost", "", "", "");
if (mysqli_connect_errno()) {
throw new Exception("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$cid = $_GET['cid'];
$tid = $_GET['tid'];
$userid = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );
//Prepare
if($stmt = mysqli_prepare($con, "SELECT * FROM forum_topics WHERE `category_id`=? AND `id`=? LIMIT 1")) {
mysqli_stmt_bind_param($stmt, "ii", $cid, $tid);
mysqli_stmt_execute($stmt);
/* fetch value */
mysqli_stmt_fetch($stmt);
if (!$stmt) {
throw new Exception($con->error);
}
}
$stmt->store_result();
$numrows = $stmt->num_rows;
if($numrows == 1){
echo "<table width='100%'>";
if ( $_SESSION['user'] ) {
echo "<tr><td colspan='2'><input type='submit' value='Add Reply' onclick=\"window.location =
'forum_post_reply.php?cid=".$cid."$tid=".$tid."'\"> <hr />";
} else {
echo "<tr><td colspan='2'><p>Please log in to add your reply</p><hr /></td></tr>";
}
/*}
catch (Exception $e)
{
//print_r($e);
echo "There has been an error with the $stmt block.";
}
print_r($stmt);
try {*/
foreach($stmt as $row) {
//Prepared SELECT stmt to get forum posts
if($stmt2 = mysqli_prepare($con, "SELECT * FROM forum_posts WHERE `category_id`=? AND `topic_id`=?")) {
//var_dump($stmt2);
mysqli_stmt_bind_param($stmt2, "ii", $cid, $tid);
mysqli_stmt_execute($stmt2);
}
while (mysqli_stmt_fetch($stmt)) {
echo "<tr><td valign='top' style='border: 1px solid #000000;'>
<div style='min-height: 125px;'>".$row['topic_title']."<br />
by ".$row2['post_creator']." - " .$row2['post_date']. "<hr />" . $row2['post_content'] ."</div></td>
<td width='200' valign='top' align='center' style='border: 1px solid #000000;'>User Info Here!</td></tr>
<tr><td colspan='2'><hr /></td></tr>";
}
}
} else {
echo "<p>This topic does not exist.</p>";
}
}
catch (Exception $e)
{
//print_r($e);
echo "There has been an error with the foreach $stmt2 block.";
}
print_r($stmt2);
如果您想跟踪代码中发生的所有错误,可以使用try、catch语句以适当的方式处理它们。在try块中,可以放置所有代码或部分代码。如果某项操作失败,它将生成一个异常 您也可以使用下一条语句引发该异常:
throw new Exception("I think something's failing");
在try块之后,您必须声明将处理异常的catch块,使其对您可用。在下面的示例中,我将打印错误消息
<?php
try {
/* all your code */
} catch (Exception $e) {
/* if something fails inside try block will be catched here */
print $e->getMessage();
}
您可以在此处找到更多关于此的信息:
我想知道我是否正确地执行了try/catch操作,以及为什么会出现$stmt2变量未定义的错误。之所以会出现$stmt2变量的错误,是因为它是在catch语句中硬编码的。使用$e->getMessage将处理使您脱离脚本正常流程的第一个异常。您能进一步解释一下还是告诉我您的意思?仍在努力学习try/catch。在我的情况下,最好有两个try块,你是说我得到了错误,因为我试图在catch之外打印$stmt2吗?我以为打印$stmt2是最后一部分?你可以有太多的try块,你也可以嵌套它们。由你决定。如果你检查我在答案中的链接,你会注意到还有一个finally块。最后,将始终执行块,无论有无错误。只有在出现异常时才会执行Catch块。
<?php
try {
/* all your code */
} catch (Exception $e) {
/* if something fails inside try block will be catched here */
print $e->getMessage();
}