imagecreatefromjpeg打破了mysql

时间:2013-06-07 21:44:59

标签: 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();

?>

1 个答案:

答案 0 :(得分:2)

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

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

例如

的mysql_connect(本地主机,$用户,$密码);

必须是

的mysql_connect( “本地主机”,$用户,$密码);

你可以运行下面的代码,看看你得到的除了图像

之外的任何输出
<?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();

?>