在动态图像中显示mysql数据

时间:2013-09-25 15:48:43

标签: php mysql

我正在研究动态用户统计信息的签名。

这是我的代码:更新

<?php
//Send a generated image to the browser
$config->Host = 'localhost';
$config->User = 'grpg';
$config->Pass = 'E8vspAcP';
$config->DB   = 'grpg_wp';;

$con = mysql_connect($config->Host,$config->User,$config->Pass);
if (!$con) die('<div class="errorbox">Nepavyko prisijungti prie duomenu bazes: '. mysql_error() .'</div>');

mysql_select_db($config->DB, $con);
$query = mysql_query("SELECT * FROM serverplayers WHERE id=1");
$showuser = mysql_fetch_assoc($query);
$user1 = $showuser['User'];
$user="$user1";  // or the value from your database
create_image($user);

function create_image($user)
{
    //Set the image width and height
    $width = 100;
    $height = 20;

    //Create the image resource
    $image = ImageCreate($width, $height);

    //We are making three colors, white, black and gray
    $white = ImageColorAllocate($image, 255, 255, 255);
    $black = ImageColorAllocate($image, 0, 0, 0);
    $grey = ImageColorAllocate($image, 204, 204, 204);

    //Make the background black
    ImageFill($image, 0, 0, $black);

    //Add randomly generated string in white to the image
    ImageString($image, 3, 30, 3, "$user", $white);
    //Tell the browser what kind of file is come in
    header("Content-Type: image/jpeg");

    //Output the newly created image in jpeg format
    ImageJpeg($image);

    //Free up resources
    ImageDestroy($image);
}
?>

如果我更改此内容:$pass = "$user";到此$pass = "erlis";我的用户名显示没有问题。这是什么交易?图像显示效果很好但没有文字。无法弄清楚......

1 个答案:

答案 0 :(得分:3)

$show['User'];不在该职能范围内。你需要将它传递给你的函数

function create_image($user) 
{
  //.....
}

并且,在填充变量之后调用函数,而不是之前!

$user="test";  // or the value from your database
create_image($user);