Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 09-08-2012, 01:50 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Select value from table where varname = '3 different things''

I am trying to select 3 items from the setting table:
the info from the value column where the varname column equals illegalusernames, minusernamelength & maxusernamelength.

Something like:
Code:
"SELECT value AS illegalusernames FROM ".TABLE_PREFIX."setting WHERE varname = 'illegalusernames'"
"SELECT value AS minusernamelength FROM ".TABLE_PREFIX."setting WHERE varname = 'minusernamelength'"
"SELECT value AS maxusernamelength FROM ".TABLE_PREFIX."setting WHERE varname = 'maxusernamelength'"
I want to do this without having to pull 3 different queries.
Reply With Quote
  #2  
Old 09-08-2012, 03:05 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Your where would be something like:
Code:
SELECT value, varname FROM `setting` WHERE varname IN ('illegalusernames','minusernamelength ','maxusernamelength')
Reply With Quote
Благодарность от:
Nullifi3d
  #3  
Old 09-08-2012, 05:36 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

For some odd reason the sql only fetches the illegalusernames field when I read the var_dump output.
Code:
$results = $db->query_first("SELECT value, varname FROM ".TABLE_PREFIX."setting WHERE varname IN ('illegalusernames','minuserlength ','maxuserlength')");
Also tried query_read, but it only outputs a mysql resource.
Reply With Quote
  #4  
Old 09-08-2012, 05:43 PM
Lynne's Avatar
Lynne Lynne is offline
 
Join Date: Sep 2004
Location: California/Idaho
Posts: 41,180
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

(The same was true when I tested it on my setting table but then I went and looked and neither of those other settings are in there.) However, if the rows are in your table, you aren't going to get them by using query_first. query_first only fetches a single row. You need to do a regular query and then a fetch_array (probably in a while loop to grab the values).
Reply With Quote
  #5  
Old 09-08-2012, 08:24 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I was doing some research and found matching information to what you're saying and tried adjusting my code:
Code:
$query = $db->query_read("SELECT value, varname FROM ".TABLE_PREFIX."setting WHERE varname IN ('illegalusernames','minuserlength ','maxuserlength')");
$results = $db->fetch_array($query);
print_r($results);

The Above Outputs:
Array ( [value] => admin moderator op9 option9 option.9 forum vbulletin [varname] => illegalusernames )
I can't understand doing a loop/while/foreach if there isn't proper array data to loop through.
Strange enough, when executing this sql straight into phpmyadmin it returns all three rows. So I assume it must be something with how I am calling the db queries within vbulletin/php.

I have tried several variations of queries/fetches with no success.
Reply With Quote
  #6  
Old 09-08-2012, 09:58 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You want to do something like:

Code:
$query = $db->query_read("SELECT value, varname FROM ".TABLE_PREFIX."setting WHERE varname IN ('illegalusernames','minuserlength ','maxuserlength')");

while ($results = $db->fetch_array($query))
{
    print_r($results);
}


Edit: Oops, just noticed that Lynne beat me to that first reply. Anyway, you also have an extra space in the 'minuserlength' string.
Reply With Quote
  #7  
Old 09-08-2012, 11:07 PM
Nullifi3d Nullifi3d is offline
 
Join Date: Apr 2004
Location: FL, USA
Posts: 215
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by kh99 View Post
You want to do something like:

Code:
$query = $db->query_read("SELECT value, varname FROM ".TABLE_PREFIX."setting WHERE varname IN ('illegalusernames','minuserlength ','maxuserlength')");

while ($results = $db->fetch_array($query))
{
    print_r($results);
}

You should think about it as if the "query" call initializes things, and the fetch_array() actually gets the data. If you had a large database and a query that returned thousands of rows, it might not all fit in memory at the same time so the function couldn't return an array. Instead it's set up this way so that you don't have to concern yourself with those kinds of issues.

Edit: Oops, just noticed that Lynne beat me to that first reply. Anyway, you also have an extra space in the 'minuserlength' string.
freaking genius. ty... i feel like a moran for not trying while.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:01 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04194 seconds
  • Memory Usage 2,219KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (6)bbcode_code
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (7)post_thanks_box
  • (1)post_thanks_box_bit
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit
  • (7)post_thanks_postbit_info
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.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
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • fetch_musername
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • post_thanks_function_fetch_thanks_bit_start
  • post_thanks_function_show_thanks_date_start
  • post_thanks_function_show_thanks_date_end
  • post_thanks_function_fetch_thanks_bit_end
  • post_thanks_function_fetch_post_thanks_template_start
  • post_thanks_function_fetch_post_thanks_template_end
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete