Jump to content

GD and MySQL


Xoom3r

Recommended Posts

Hey there people, first I'm gonna post my code, then I will explain.

<?php
if(isset($_GET['player'])){
$player = $_GET['player'];
} else { $player = "N/A"; }
require_once("config/database.php");

$accounts = mysql_query("SELECT * FROM accounts");
$saccounts = mysql_num_rows($accounts);
$characters = mysql_query("SELECT * FROM characters");
$scharacters = mysql_num_rows($characters);
$guilds = mysql_query("SELECT * FROM guilds");
$sguilds = mysql_num_rows($guilds);
$banned = mysql_query("SELECT * FROM accounts where banned = 1");
$sbanned = mysql_num_rows($banned);
$online = mysql_query("SELECT * FROM accounts where loggedin = 2");
$sonline = mysql_num_rows($online);
$offline = mysql_query("SELECT * FROM accounts where loggedin = 0");
$soffline = mysql_num_rows($offline);
$gms = mysql_query("SELECT * FROM accounts where gm = 100");
$sgms = mysql_num_rows($gms);
// SHOW THE FUCKING IMAGE
header ("Content-type: image/png");
$im = @ImageCreate (256, 64)
or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 0, 0, 0);
$text_color = ImageColorAllocate ($im, 255, 255, 255);
$online_color = ImageColorAllocate ($im, 34, 177, 76);
$offline_color = ImageColorAllocate ($im, 255, 0, 0);

// DEFAULT TEXT
$text = 'rafl';

// FONT PATH
$font = 'visitor.ttf';
$font1 = 'cheri.ttf';

// GMs
imagettftext($im, 8, 0, 5, 10, $text_color, $font, "GMs:");
imagettftext($im, 8, 0, 80, 10, $text_color, $font, $sgms);
// Guilds
imagettftext($im, 8, 0, 5, 21, $text_color, $font, "Guilds:");
imagettftext($im, 8, 0, 80, 21, $text_color, $font, $sguilds);
// Characters
imagettftext($im, 8, 0, 5, 33, $text_color, $font, "Characters:");
imagettftext($im, 8, 0, 80, 33, $text_color, $font, $scharacters);
// Accounts
imagettftext($im, 8, 0, 5, 45, $text_color, $font, "Accounts:");
imagettftext($im, 8, 0, 80, 45, $text_color, $font, $saccounts);
// Banned
imagettftext($im, 8, 0, 5, 57, $text_color, $font, "Banned:");
imagettftext($im, 8, 0, 80, 57, $text_color, $font, $sbanned);

// HALF LINE
imagettftext($im, 8, 0, 100, 10, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 15, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 21, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 27, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 33, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 38, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 45, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 50, $text_color, $font, "|");
imagettftext($im, 8, 0, 100, 57, $text_color, $font, "|");

//ONLINE
imagettftext($im, 8, 0, 115, 10, $online_color, $font, "Online users:");
imagettftext($im, 8, 0, 190, 10, $online_color, $font, $sonline);
//OFFLINE
imagettftext($im, 8, 0, 115, 21, $offline_color, $font, "Offline users:");
imagettftext($im, 8, 0, 195, 21, $offline_color, $font, $soffline);

imagettftext($im, 20, 0, 125, 50, $text_color, $font1, $player);

ImagePng ($im);
?> 

 

This creates an image that takes some data from a MySQL database.

Though, my mind is blowing right now. I am try to get data from a column in a specific row,

but it doesn't seem to work!

I did not include the code that's described above because it's too embarrassing .

Can someone please help me code something like this?

 

SELECT FROM accounts WHERE name = $player  --> the row that has in column "name"  $player, show data that's in a column named "level"

(this is valid until "-->", but i want it to continue like this and i don't know how)

 

I hope I was clear enough. Please help me.

 

Link to comment
https://forums.phpfreaks.com/topic/189227-gd-and-mysql/
Share on other sites

<?php
if(isset($_GET['player'])){
$player = $_GET['player'];
} else { $player = "N/A"; }
require_once("config/database.php");

$level = mysql_query("SELECT * FROM characters WHERE name = $player");
while ($row = mysql_fetch_assoc($level)) 
{
$row["level"];
}; 
........

eh, my gd shows an error.

Link to comment
https://forums.phpfreaks.com/topic/189227-gd-and-mysql/#findComment-998975
Share on other sites

Right, because the name column is a string so your query needs to be:

 

"SELECT * FROM characters WHERE name = '$player'"

 

It's always good to check the return value after you query.

 

$level = mysql_query("SELECT * FROM characters WHERE name = $player");
if ($level) {
  // fetch
} else {
  // There was an error.  
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/189227-gd-and-mysql/#findComment-999068
Share on other sites

Cool worked that out, here's the code, though I have another question.

How can I display an external image on my GD image?

 

$sql = "SELECT * FROM characters WHERE name = '$player'";
$res = mysql_query($sql) or die(mysql_error);
if (mysql_num_rows($res) == 0) {
    $avatar = "x.gif";
    $username = "Error";
    $group = "Error";
    $posts = "Error";
} else {
    $row = mysql_fetch_assoc($res);
    $level = $row['level'];
    $username = $row['name'];
$meso = $row['meso'];
$reborn	= $row['reborns'];
$exp	= $row['exp'];
}

 

 

Link to comment
https://forums.phpfreaks.com/topic/189227-gd-and-mysql/#findComment-999310
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.