尝试抓住准备好的陈述 [英] try and catch with a prepared statement

查看:90
本文介绍了尝试抓住准备好的陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在执行此查询时遇到了很多困难,建议我学习尝试捕获块以更轻松地找出问题所在.所以这是我第一次尝试.

I have had a very hard time with this query and it was suggested to me to learn try and catch blocks to more easily figure out what is wrong. So this is my first attempt at it.

我在未为print_r($stmt2)行定义@ stmt2时遇到错误.

I am getting the error for my @stmt2 not being defined for my print_r($stmt2) line.

Notice: Undefined variable: stmt2 

这是我尝试的尝试.我要为这个错误做任何事情吗?

This is my attempt at the try catch. Am I doing anything wrong with it for this error to come up?

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块中,您可以放置​​所有代码或部分代码.如果某件事失败,它将生成一个例外.

If you wanna track all the errors that happen in your code, you can use try, catch statements to handle them in proper way. Inside try block, you can put all your code or part of it. If something fails, it will generate an exception.

您也可以使用下一条语句抛出该异常:

That exception also can be thrown by yourself using the next statement:

throw new Exception("I think something's failing");

try 块之后,您必须声明 catch 块,该块将处理异常,使您可以使用它.在下面的示例中,我将打印错误消息.

Just after try block, you have to declare the catch block which will handle the exception, making it available to you. In the following example I will print the error message.

<?php

try {
    /* all your code */
} catch (Exception $e) {
   /* if something fails inside try block will be catched here */
    print $e->getMessage();
}

您可以在这里找到更多有关此的信息: http://php.net/manual/zh/language.exceptions.php

You can find more about this here: http://php.net/manual/en/language.exceptions.php

这篇关于尝试抓住准备好的陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆