php - imagecreatefromjpeg 与 mysql 中断

标签 php mysql

我有一个 PHP 脚本(如下),它使用 imagecreatefromjpeg 并且它可以工作。然后我添加了几个 mysql 语句,它不再起作用(我看到损坏的图像图标)。

如何混合 imagecreatefromjpeg 并与 mysql 查询相关。谢谢!

<?php
//Report any errors
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$user="root";
$password="pw";
$database="piwigo";

//Adding these two lines breaks the image:
  //mysql_connect(localhost,$user,$password);
  //@mysql_select_db($database) or die( "Unable to select database");

$current_image = imagecreatefromjpeg("sample.jpg");
// place code for saving the montage image as a file or outputting to the
// browser here.
header("Content-type: image/jpeg"); 
imagejpeg($current_image);
imagedestroy($current_image);

mysql_close();

?>

最佳答案

//Report any errors
ini_set ("display_errors", "1");
error_reporting(E_ALL);

您报告错误,它会生成输出然后图像损坏。

例如

mysql_connect(localhost,$user,$password);

必须是

mysql_connect("localhost",$user,$password);

您可以运行下面的代码来查看是否获得除图像之外的任何输出

<?php
//Report any errors
ini_set ("display_errors", "1");
error_reporting(E_ALL);

$user="root";
$password="pw";
$database="piwigo";

//Adding these two lines breaks the image:
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

// $current_image = imagecreatefromjpeg("sample.jpg");
// place code for saving the montage image as a file or outputting to the
// browser here.
// header("Content-type: image/jpeg"); 
// imagejpeg($current_image);
// imagedestroy($current_image);

mysql_close();

?>

可能的工作代码

<?php
// Don't Report any errors
error_reporting(0);

$user="root";
$password="pw";
$database="piwigo";

//Adding these two lines breaks the image:
mysql_connect("localhost",$user,$password) or die("Unable to connect database");
@mysql_select_db($database) or die("Unable to select database");

$current_image = imagecreatefromjpeg("sample.jpg");
// place code for saving the montage image as a file or outputting to the
// browser here.
header("Content-type: image/jpeg"); 
imagejpeg($current_image);
imagedestroy($current_image);

mysql_close();

?>

关于php - imagecreatefromjpeg 与 mysql 中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993447/

相关文章:

php - 在 PHP 中,从基类的实例创建扩展类的对象

php - 如果可能的话,使用 PHP 从 JSON 格式的两个 MySQL 表中获取数据

php - 设置 php 子句中 html 标签属性的值

php - 是否可以在单个查询中选择 MonthToDate、LastYearMonthToDate、YearToDate 和 LastYearToDate?

php - 单击复选框时会出现一个文本框,适用于 jsfiddle,但不适用于我的浏览器

php - 在 PHP while 循环期间页面不会加载并且服务器崩溃

php - 如何与PHP一起显示带有限制的mysql数组

PHP foreach循环使用PDO将数据插入mysql仅插入第一行

php - 跨多个字段的搜索查询,包括 JOIN

php - 如何获取数据库中没有的文件