Page 1 of 1

Lanzer's answers

Posted: Wed Dec 21, 2005 1:55 pm
by lanzer
Hi Capleton:

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 = '';
   }
}
3) But of course, fundementally you should ask yourself what do you want to do, or give the reader an explaination of what you're trying to accomplish when you asked the question. If this code will be applied to viewtopic.php, then chances are you don't want to run this query for 15 times as each topic posting is being iterated. Nor would you want to have the query check by username when you can probably use user_id. Though no one can give a better answer without knowing how the $poster vaiable is provided, and related information as to how the query is used.

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]