1

I have a problem. I am trying, but I am not getting the exact solution.

I have code to take something from database.

try {
    require_once('blogic.php');
    $obj  = new blogic();
    $re   = $obj->select("SELECT link FROM eff ORDER BY RAND() LIMIT 1");
    $l    = mysql_fetch_row($re);
    $link = $l[0];
} catch (Exception $e) {
    $link = "http://www.xyz.com/friendsin2013/";
}

The above code is working properly, but if (for whatever reason) any problem comes while getting data from the database, I want the link to be defaulted as given in the catch block.

However it is not working the way I want it to. It works when there is no error, however, when I experience an errorr, $link does not get passed from the catch. I just get the error message instead.

Blogic.php life is like --->> This is a file to get database entry.

<?php

include('s.php');

class blogic {

    function connect() {
        $link = mysql_connect(SERVER, USER, PASSWORD);
        if (!$link) {
            die('Could not connect: ' . mysql_error());
            return false;
        } else {
            return true;
        }
    }

    function select_database() {
        $db = mysql_select_db(DATABASE);
        if (!$db) {
            die('Could not connect: ' . mysql_error());
            return false;
        } else {
            return true;
        }
    }

    function select($str) {
        self::connect();
        self::select_database();

        $res = mysql_query($str);
        return $res;
    }
}
?>

s.php ->>

4

2 回答 2

0

是在/逻辑$link之前定义的吗?如果不先尝试。trycatch

$link使用提供的代码看起来无法在try/之外访问。catch

尽管马特以最佳答案击败了我;和他一起去是完全可能的。

于 2012-08-28T18:09:57.463 回答
0

把它写成一个函数,然后调用它;$linktrycatch块中返回:

require_once('blogic.php');

function getLink() {
     try {
        $obj = new blogic();
        $re=$obj->select("SELECT link FROM eff ORDER BY RAND() LIMIT 1");
        $l=mysql_fetch_row($re);
        return $l[0];
    }
    catch(Exception $e) {
        return "http://www.xyz.com/friendsin2013/";
    }
}

$link = getLink();
于 2012-08-28T18:02:39.237 回答