Version: , by JJR512
Developer Last Online: Jun 2006
Version: Unknown
Rating:
Released: 02-24-2002
Last Update: Never
Installs: 0
No support by the author.
Suppose I have a table with a bunch of columns. The first is userid. A row gets added to the database when a user goes through and fills out a form. Prior to the user filling out the form, there is no row in the database with his/her userid.
When the form is displayed, I need to determine whether or not the person who requested the form to be displayed has a row in that table, so the code can determine whether to set the various fields to some default values (if there is no row for that user) or to the values the user already selected.
So far, I've come up with this:
PHP Code:
$usersettings = $DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]");
if ($usersettings[userid]="") {
// do stuff
} else {
// do other stuff
}
It works. But for some reason I've got this weird feeling that there's a better way. Can someone please let me know what that better way is, or else put my mind at ease that I'm doing it the right way now?
Show Your Support
This modification may not be copied, reproduced or published elsewhere without author's permission.
if ($usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
// user is there
} else {
// user is not there
}
/* ### OR ### */
$usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")
if ($DB_site->num_rows()>0) {
// user is there
} else {
// user is not there
}
Well, I was having some trouble doing testing to see if I could answer my own questions. So I tried this, and this works too, and unless you tell me there's any particular reason why this isn't a good idea, I'll do this:
PHP Code:
$usersettings = $DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]");
if (!isset($usersettings[userid])) {
// user NOT in table
} else {
// user IS in table
}
It works, but I find my first method more elegant (less lines, I guess that's why).
No, you shouldn't pass $usersettings to num_rows() because it's not a result set, it's an associative array. If you call num_rows() without any parameters it will use the last executed query, which is what we want.
And turn it into <1 if want to see if the user is not there.
I guess the reason why I didn't use the first method was that I need the query executed separately from the if...else block. I also need the if...else block to be in the order of if the user isn't there, else if the user is there. The reason for this is that if the user is not there, variables for $usersettings ($usersettings[userid], usersettings[setting1], etc.) are explicitly defined with a set of default values in the "if user isn't there" part of the code. I actually changed it so it isn't an if...else, just an if. If the user isn't there, the default values are put into the variables, and either way, execution procedes to the next part, using either the default values or the values from the query.