Php 在类中组织代码时,数据库中的图像不显示
当我在类中组织代码时,从数据库中获取的图像有问题 这是有效的代码:Php 在类中组织代码时,数据库中的图像不显示,php,sql,database,image,mysqli,Php,Sql,Database,Image,Mysqli,当我在类中组织代码时,从数据库中获取的图像有问题 这是有效的代码: $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[
$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"];
$_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"];
[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;
}
}
提前谢谢 代码适合我。唯一的可能性是,$\u GET没有移交给
imagen.php
要测试请将$\u GET[“id”]设置为激活的id [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"];
$_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"];
如果没有连接,也不要让代码运行一个简单的返回是不够的。
在返回之前将
$this->\u db
设置为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;
代码对我很有用。唯一的可能性是,$\u GET没有移交给
imagen.php
要测试请将$\u GET[“id”]设置为激活的id [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"];
$_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"];
如果没有连接,也不要让代码运行一个简单的返回是不够的。
在返回之前将
$this->\u db
设置为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;
我修好了,伙计们
我不得不删除require config.php(包含连接、所有其他类和函数的文件),并将其替换为显示图像所需的文件。我建议把config.php中的所有内容都写下来,因为我不想把问题写得太长,但我猜这些文件中有什么东西破坏了图像或其他东西
再次感谢您有时间阅读并回复。我有一些很棒的建议 我修好了,伙计们
我不得不删除require config.php(包含连接、所有其他类和函数的文件),并将其替换为显示图像所需的文件。我建议把config.php中的所有内容都写下来,因为我不想把问题写得太长,但我猜这些文件中有什么东西破坏了图像或其他东西
再次感谢您有时间阅读并回复。我有一些很棒的建议 尝试捕获异常-您的代码假定调用按预期返回。函数返回false以允许您检查和处理错误。是否您在
conexion
类开始时定义的常量实际上不是该类的一部分,因此在调用imagen
类中的父项::\uu construct()
方法时不会进行初始化?不要这样做:“SELECT*FROM`imagenes`WHERE id\u imagenes=“.$\u GET[“id”]
。注入潜力。伙计们!感谢你们的帮助,我已经修改了你们给我的所有安全提示,但这是一个可怕的恶作剧!当我做回显时,变量会从基部带来数据,并将它们带出标题(“内容类型:…$imagen[“tipo\u imagenes”]和$imagen[“imagen_imagenes”];但它没有转换为映像!尝试捕获异常-您的代码假定调用按预期返回。函数返回false以允许您检查和处理错误。可能是因为您在conexion
类开始时定义的常量实际上不是类的一部分,因此在您调用imagen
类中的父项::\u construct()
方法?不要执行此操作:“从`imagenes`中选择*WHERE id\u imagenes=“.$\u GET[“id”]
.Injection potential.伙计们!感谢你们的帮助,我修改了你们给我的所有安全提示,但这是一个可怕的恶作剧!当我做回显时,变量会从基部带来数据,并将它们带出标题(“内容类型:…$imagen[“tipo_imagenes”]和$imagen[“imagen_imagenes”];但它没有转换成图像!伙计们!感谢你们的帮助,我修改了你们给我的所有安全提示,但这是一个可怕的恶作剧!当我做回显时,变量会从基础中带来数据,将它们从标题中带出(“内容类型:…$imagen[“tipo_imagenes”]和$imagen[“imagen_imagenes”];但它没有转换成图像!伙计们!感谢你们的帮助,我修改了你们给我的所有安全提示,但这是一个可怕的恶作剧!当我做回显时,变量会从基础中带来数据,将它们从标题中带出(“内容类型:…$imagen[“tipo_imagenes”]和$imagen[“imagen_imagenes”];但它并没有转化为图像!