dslk6326846 2015-07-07 01:49
浏览 58

如果数据库连接失败则加载默认图像

I am attempting to build a database driven site whereby images are loaded via a php script like so;

<img src="get_image.php?holderID=2">

I can get images to load from a folder outside the root directory when the database is accessible but I also want to be able to load a default image if there is a failure with making the database connection. The DB connection is initiated form a separate php connection file mysqli_template_connect.php;

DEFINE('DB_USER', 'someusername');
DEFINE('DB_PASSWORD', 'amnesia');
DEFINE('DB_HOST', 'localhost');
DEFINE('DB_NAME', 'template');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

and then config.inc.php sets some constants, one of which is for all DB connections;

define('MYSQL', '../../../dbconnect/mysqli_template_connect.php');

The get_image.php file then has a database connection conditional;

    require('includes/config.inc.php');
    REQUIRE(MYSQL);
    $holderID = $_GET['holderID'];

    if(!$dbc){
        $image_name = 'img/unavailable.png';
        $info = getimagesize($image_name);  
        header("Content-Type: {$info['mime']}
");
        readfile($image_name);
    }
    else {
        $query = "SELECT imageID FROM image_holder WHERE image_holderID = $holderID ";
        $result = @mysqli_query($dbc, $query);
        $number_rows = mysqli_num_rows($result);

        if ($number_rows == 1) {                  
          $row = mysqli_fetch_array($result, MYSQLI_NUM);
          $imageID = $row[0];
        }
        else {
          $imageID = FALSE;
        }

        if ($imageID) {
            $query = "SELECT file_name FROM image WHERE imageID = $imageID";
            $result = mysqli_query($dbc, $query);
            $number_rows = mysqli_num_rows($result);
            if ($number_rows == 1) {
                $row = mysqli_fetch_array($result, MYSQLI_NUM);
                $image_name = '../../../uploads/' . $row[0];
            }
            else {
                $image_name = 'img/unavailable.png';
            }
        }
        else {
            $image_name = 'img/unavailable.png';
        }

        $info = getimagesize($image_name);  
        header("Content-Type: {$info['mime']}
");
        readfile($image_name);
        mysqli_close($dbc);

    }

If I disable the MYSQL database in XAMP, the default image unavailable.png will not load even though the header and readfile section of code is virtually the same in the section of code that does work. I'm quite a newbie to all this so any ideas on loading the default image would be appreciated.

  • 写回答

3条回答 默认 最新

  • douchuifk90315 2015-07-07 02:03
    关注

    Instead of

    $dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    

    Use

    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    

    hopefully it should work..

    评论

报告相同问题?

悬赏问题

  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构