vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Member Archives (https://vborg.vbsupport.ru/forumdisplay.php?f=202)
-   -   What is the best way to see if a row is NOT in the database? (https://vborg.vbsupport.ru/showthread.php?t=35394)

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:
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?

Admin 02-24-2002 04:49 PM

Code:

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:
PHP Code:

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:
PHP Code:

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:
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
  



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:
Code:

if ($DB_site->query_first("SELECT * FROM weather_usersettings WHERE userid=$bbuserinfo[userid]")) {
and:
Code:

if ([high]$usersettings=[/high]$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. :)


All times are GMT. The time now is 09:02 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01085 seconds
  • Memory Usage 1,738KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (4)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete