首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在类中组织代码时数据库中未显示的图像。

在类中组织代码时数据库中未显示的图像。
EN

Stack Overflow用户
提问于 2015-12-05 22:36:44
回答 2查看 46关注 0票数 1

当我在类中组织我的代码时,我从数据库中获得的图像出现了问题。

这是工作的代码:

代码语言:javascript
复制
$mysqli=new mysqli("XXXX","XXXX","XXX","XXXXXX");
if (mysqli_connect_errno()) {
    die("Error al conectar: ".mysqli_connect_error());
}

$result=$mysqli->query("SELECT * FROM `imagenes` WHERE id_imagenes=".$_GET["id"]);
$row=$result->fetch_array(MYSQLI_ASSOC);

header("Content-type:".$row["tipo_imagenes"]);
echo $row["imagen_imagenes"];

但当我把它弄成这样,它就不起作用了。它显示了破损的图像图标:

imagen.php

代码语言:javascript
复制
require('../includes/php/config.php');

$imagen_trae= new imagen(); 
$imagen = $imagen_trae->traerImagen(); 

header("Content-type:".$imagen["tipo_imagenes"]);
echo $imagen["imagen_imagenes"];

config.php

代码语言:javascript
复制
require('conexion.php');

include_once('imagen_class.php');

conexion.php

代码语言:javascript
复制
define('DB_HOST','xxxxx'); 
define('DB_USER','xxxx'); 
define('DB_PASS','xxxxx'); 
define('DB_NAME','xxxx'); 
define('DB_CHARSET','utf-8'); 

class conexion 
{ 
    protected $_db; 

    public function __construct() 
    { 
        $this->_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 

        if ( $this->_db->connect_errno ) 
        { 
            echo "Fallo al conectar a MySQL: ". $this->_db->connect_error; 
            return;     
        } 

        $this->_db->set_charset(DB_CHARSET); 
    } 
} 

imagen_class.php

代码语言:javascript
复制
class imagen extends conexion 
{ 

    public function __construct() 
    { 
        parent::__construct(); 
    } 

    public function traerImagen() 
    {
        $result=$this->_db->query("SELECT * FROM imagenes WHERE id_imagenes=".$_GET["id"]);
        $imagenes_todas=$result->fetch_array(MYSQLI_ASSOC);

        return $imagenes_todas; 
    }
} 

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-06 06:43:22

密码对我有用。

唯一的可能是$_GET不被移交给imagen.php

若要测试将$_GET"id“设置为Excisting id

imagen.php

代码语言:javascript
复制
$_GET["id"]=existing_id;
require('../includes/php/config.php');

$imagen_trae= new imagen(); 
$imagen = $imagen_trae->traerImagen(); 

header("Content-type:".$imagen["tipo_imagenes"]);
echo $imagen["imagen_imagenes"];

如果没有连接,也不要让代码运行!!

一个简单的返回是不够的。

在返回之前, null 设置为null

代码语言:javascript
复制
class conexion 
{ 
protected $_db; 

public function __construct() 
{ 
    $this->_db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME); 

    if ( $this->_db->connect_errno ) 
    { 
        echo "Fallo al conectar a MySQL: ". $this->_db->connect_error; 
        $this->_db = null;
        return;     
    } 

和测试它

代码语言:javascript
复制
class imagen extends conexion 
{ 

public function __construct() 
{ 
    parent::__construct(); 
} 

public function traerImagen() 
{
    if ($this->_db != null) {
    $result=$this->_db->query("SELECT * FROM imagenes WHERE id_imagenes=".$_GET["id"]);
    $imagenes_todas=$result->fetch_array(MYSQLI_ASSOC);

    return $imagenes_todas; 
    } else return null; 
票数 0
EN

Stack Overflow用户

发布于 2015-12-06 15:32:13

我修好了伙计们!

我不得不删除require config.php (具有对分、所有其他类和函数的文件),并将其替换为显示图像所需的文件。我省略了所有我在config.php中拥有的东西,因为我不想把我的问题提得太久,但我想这些文件中的某些东西破坏了图像或什么的。

再次感谢您有时间阅读并回复。我得到了一些很棒的建议!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34111740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档