There are several answers to your question, let's look at the problem one by one -
1) Your code right now sets the variable as empty if your query fails. Meanwhile, if your query suceed, it set variable to the image string if your query returns a row of results while it does nothing at all if no results are found. I don't think that's the action you want.
2) You probably want to use $db->sql_numrows() to check and see if the query returns zero row result or not. Depending on the result, you then set the variable. You don't need a while loop at all.
Code: Select all
if ($result = $db->sql_query($sql)) {
if ( !empty($db->sql_numrows($result) ) {
$variable = '<img src="http://url" alt="img">';
} else {
$variable = '';
}
}
Good luck!
-Derek
[quote="capleton"]
Hi, this isn't exactly a tweak but thought i ask here since you guys seems to be experts in SQL.
I've done a script that checks if a username is in a db and if it is setting a variable to contain an img, else set an empty var, if there's a better way to do this query or better yet, implement it at some other place in viewtopic.php.
Problem is it set's the var for some users that shouldn't have the var set, im gonna post the code and see if you can spot something wrong with it.
Code: Select all
$sql = "SELECT *
FROM phpbb_column
WHERE username = '$poster'";
if ( !$db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Could not fetch lövenbyggare.", '', __LINE__, __FILE__, $sql);
}
if ($result = $db->sql_query($sql)) {
while($row = $db->sql_fetchrowset($result)) {
$variable = '<img src="http://url" alt="img">';
}
}
else {
$variable = '';
}
$db->sql_freeresult($result);
Thankful if someone could help out!
[/quote][/code]