Page 1 of 1

Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 10:26 am
by goodrichdesigns
Hi all, I'm still struggling with If/Else PHP script. It's calling values from a MySQL database. It's for an assignment which is due in on Tuesday, can anyone please help? :?

Code: Select all

<?PHP
if ($player->avatar == 0) echo "No avatar";
else print'<img src="'.$player['avatar'] .'" alt="'.$player['username'] .'">';
 ?>

The default value for the avatar field of my database is 0. If the value is 0 I want text to be outputted on the page reading 'No Avatar'. If there is something else e.g. an URL, I want this to be printed as an image on the page.

Re: Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 10:29 am
by onion2k

Code: Select all

<?PHP
if ($player->avatar == 0) echo "No avatar";
else print'<img src="'.$player['avatar'] .'" alt="'.$player['username'] .'">';
?>
On the first line you're using object notation (eg $player->avatar), and on the second you're using array notation (eg $player['avatar']). One of them is wrong. Which one it is depends on the rest of your code.

Re: Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 10:45 am
by goodrichdesigns
I think the object notation is the correct one, but now how do I print an image as an 'else' statement in object notation form? (Sorry I'm very new to PHP/MySQL)

Re: Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 1:12 pm
by onion2k
Take a moment to think about it, try a few things and see if you're right. If you're not, post your code here and we'll help you see what you did wrong.

Coding is all about thinking things through. If you're not willing to do that you'll never be able to write anything.

Re: Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 1:34 pm
by goodrichdesigns
I've been working with this piece of code for over a week! I've been wrestling with it for over four hours today. Here is another attempt I've made:

<?php
$query = $db->execute("select `id`, `username`, `avatar` from `players` ?");


if ($player->avatar == 0)
{
echo "No avatar";
}
else
{
echo "YES!";
}
?>


It seems as though it is echoing 'No avatar' even if the value is not 0. I just can't get it to work.

Re: Need help printing an image URL from a MySQL database

Posted: Wed Apr 02, 2008 3:28 pm
by onion2k

Code: Select all

$query = $db->execute("select `id`, `username`, `avatar` from `players` ?");
if ($player->avatar == 0)
{
echo "No avatar";
}
else
{
echo "YES!";
}
?>
Several problems here:

1. Why is there a ? at the end of your SQL query?

2. How are you getting from $query holding the result of the database query to $player being an object holding content from a row in $query? Without knowing which database abstraction layer you're using I can't give you the code to get the row, but if it was ADODB or ADODB Lite you'll need "$player = $query->FetchNextObj();".

3. Instead of just outputting no or yes, output $player->avatar as well. Eg

Code: Select all

echo "No avatar: ".$player->avatar;
If it's not empty then you'll see what it contains.

It is empty mind you, the problem is to do with the query I reckon, otherwise your script would echo Yes.