Help with Databases and strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Help with Databases and strings

Post by satory »

Hi, the following is my code:

Code: Select all

<?php
$con = mysql_connect('localhost', "admin","admin");
 
$db_selected = mysql_select_db('forum', $con);
 
$stringName = $_GET['name']; //Get forum users name
 
//Get the users Rank from the phpbb users DB  
$query1 = sprintf("SELECT user_rank FROM phpbb_users WHERE username_clean = '%s'", mysql_real_escape_string($stringName));
$resDepRank = mysql_query($query1,$con);
echo $resDepRank;
 
//Get the users Rank image from the phpbb_ranks DB
$DepRank=mysql_query("SELECT rank_image FROM phpbb_ranks WHERE rank_id='$resDepRank'", $con);
echo $DepRank;
 
?>
and this is the current output:
Resource id #3Resource id #4
when I want it to be
3rank1.png
where "3" is a numerical value, the rank id number from a database and "rank1.png" is a string taken from another table.

How do I change the Resource id #3 to be what I want?

oh and the database is correct with the information that I need, im not sure where this resource id thing is coming from?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Help with Databases and strings

Post by Eran »

You should read the documentation on mysql_query() - http://us2.php.net/manual/en/function.mysql-query.php
It returns a resource to be passed to one of several retrieval functions such as mysql_fetch_assoc()
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

Thank you very much.
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

Just another quick question, rather than start a whole new thread on it....

I have obtained "rank1.png" from the table it was in, and I then added that to a file path string, giving me on my local server
How do I get the image to show on the screen?

Code: Select all

//Get the users Rank image from the phpbb_ranks DB
$resQuery2 = mysql_query("SELECT rank_image FROM phpbb_ranks WHERE rank_id='$resDepRank'", $con);
$ResultRank2 = mysql_fetch_assoc($resQuery2);
$DepRank = $ResultRank2["rank_image"];
 
//Insert the address of the rank image
$str_Rank = "http://localhost/Forum/images/ranks/" . $DepRank; 
$img_Rank = imageCreateFromPNG($str_Rank);
 
ImagePng ($img_Rank); 
 
I've had no luck trying this way.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help with Databases and strings

Post by jackpf »

1. What do you see in the browser?
2. Do you have errors turned on?
3. You could try sending an image/png content type header.


:)
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

Sorry, I wasn't descriptive at all about my problem :oops:


Yes I'm using the image content header at the top of the file, and when I load the page it only shows a blank, empty image box.

How do you mean errors turned on? Its only my second day of exploring php properly, so you'll have to excuse me. I'm using notepad++ to code then uploading the file onto a local server while I mess about and test it.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help with Databases and strings

Post by jackpf »

Try putting this at the top of the script:

Code: Select all

ini_set('display_errors', E_ALL);
And just to make sure, you are looking at the actual script right, not through an <img> tag? :)
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

:) Yeah Im only looking at it through the browser's address bar, and not through any img tags...

No errors, or anything else out of the ordinary. I know the file exists as I threw it there this morning. and I've used "echo" to make sure that the file name is whats coming out of the query.

Im going to include the whole script, its not much.

Code: Select all

<?php
ini_set('display_errors', E_ALL);
Header ("Content-type: image/png");
$con = mysql_connect('localhost', "admin","admin");
 
$db_selected = mysql_select_db('forum', $con);
 
//Get forum users name
$stringName = $_GET['name']; 
 
 
//Get the users Rank from the phpbb users DB  
$query1 = sprintf("SELECT user_rank FROM phpbb_users WHERE username_clean = '%s'", mysql_real_escape_string($stringName));
$resQuery1 = mysql_query($query1,$con);
$ResultRank = mysql_fetch_assoc($resQuery1);
$resDepRank = $ResultRank["user_rank"];
 
//Get the users Rank image from the phpbb_ranks DB
$resQuery2 = mysql_query("SELECT rank_image FROM phpbb_ranks WHERE rank_id='$resDepRank'", $con);
$ResultRank2 = mysql_fetch_assoc($resQuery2);
$DepRank = $ResultRank2["rank_image"];
 
//Insert the address of the rank image
$str_Rank = 'http://localhost/Forum/images/ranks/' . $DepRank; 
$img_Rank = imageCreateFromPNG($str_Rank);
 
 
mysql_close($con);
ImagePng ($img_Rank); 
ImageDestroy ($img_Rank);
?>
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help with Databases and strings

Post by jackpf »

Well, your script works.

I'd say it was something to do with the image.

What does $str_Rank contain?


This is rather odd...definitely no errors?
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

$str_Rank contains the path to the image, in this case:

http://localhost/Forum/images/ranks/rank1.png
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help with Databases and strings

Post by jackpf »

And that image definitely exists?


I must admit, I'm a bit lost on this one. Sorry :?
satory
Forum Newbie
Posts: 7
Joined: Sat Aug 22, 2009 9:54 am

Re: Help with Databases and strings

Post by satory »

Yeah that image exists :(

Thanks for all your help so far!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Help with Databases and strings

Post by jackpf »

Right...last stab.

Try commenting out the header('Content-Type..... line. Do you get any errors now?
Post Reply