当我在类中组织我的代码时,我从数据库中获得的图像出现了问题。
这是工作的代码:
$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
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
require('conexion.php');
include_once('imagen_class.php');conexion.php
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
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;
}
} 提前感谢!
发布于 2015-12-06 06:43:22
密码对我有用。
唯一的可能是$_GET不被移交给imagen.php
若要测试将$_GET"id“设置为Excisting id
imagen.php
$_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
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;
} 和测试它
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; 发布于 2015-12-06 15:32:13
我修好了伙计们!
我不得不删除require config.php (具有对分、所有其他类和函数的文件),并将其替换为显示图像所需的文件。我省略了所有我在config.php中拥有的东西,因为我不想把我的问题提得太久,但我想这些文件中的某些东西破坏了图像或什么的。
再次感谢您有时间阅读并回复。我得到了一些很棒的建议!
https://stackoverflow.com/questions/34111740
复制相似问题