PDA

View Full Version : What is the best way to see if a row is NOT in the database?


JJR512
02-24-2002, 04:40 PM
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:
$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?

Admin
02-24-2002, 04:49 PM
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
}
Very simple. :)

Admin
02-24-2002, 04:52 PM
You can also add an exclamation mark before $usersettings in the first method, to check if the user is not there.

JJR512
02-24-2002, 05:09 PM
Question: Using your second method, in this line:
if ($DB_site->num_rows()>0) {
Does the query variable, $usersettings, not need to go in between the ()'s of the function num_rows()?

I mean, should it look like this:
if ($DB_site->num_rows($usersettings)>0) {

Or is it correct exactly as you have it? Just making sure. :)

JJR512
02-24-2002, 05:11 PM
Also, I would be making the first part be for if the user is NOT there, so instead of being >0, should it be =0 or ==0?

JJR512
02-24-2002, 05:22 PM
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:
$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
}

Admin
02-24-2002, 05:46 PM
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. :)

JJR512
02-24-2002, 06:35 PM
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.

Admin
02-25-2002, 04:53 AM
But the query is executed! :)

There's a difference between this:
if ($DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
and:
if ($usersettings=$DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
The first one only checks if the query returned a row, but the second one also stores what's returned in the variable.

I'm using this all the time, works great. :)