try and catch with a prepared statement

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 07:24:01

问题


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.

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);

回答1:


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");

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();
}

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



来源:https://stackoverflow.com/questions/31553318/try-and-catch-with-a-prepared-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!