vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   [SOLVED] SQL Help (https://vborg.vbsupport.ru/showthread.php?t=263992)

ZERO <ibis> 05-22-2011 12:06 AM

[SOLVED] SQL Help
 
I am running into a strange problem where the function below is not working:

*Note that $userid is feeding the correct value

PHP Code:

$sbquery2 $db->query_read("
            SELECT " 
TABLE_PREFIX "user.membergroupids
            FROM " 
TABLE_PREFIX "user
            WHERE " 
TABLE_PREFIX "user.userid = $userid
        "
);
        
        
$sbrow2 $db->fetch_array($sbquery2);
        
$sbmemgroups $sbrow2['membergroupids'];
    }

    
$sbusergroups explode(","$sbmemgroups); 

I am running this on paidsub_build and paidsub_delete.

Note that the explode works fine in useradmin_update_save where it is fed
PHP Code:

$userdata->fetch_field('membergroupids'

This leads me to believe that I am doing something wrong with the sql however it does not generate any errors.

Thanks for the help ;)

kh99 05-22-2011 12:10 AM

My guess is that the array key is TABLE_PREFIX . "user.membergroupids" and not just "membergroupids".

ETA: If that's the problem, the easiest thing to do would be to just change the first line of SQL to read "SELECT membergroupids".

ZERO <ibis> 05-22-2011 12:21 AM

I tried:

PHP Code:

$sbmemgroups $sbrow2['" . TABLE_PREFIX . "user.membergroupids']; 

and

PHP Code:

$sbmemgroups $sbrow2['user.membergroupids']; 

both did nothing and did not produce any errors.

Edit: i will try your edited fix now

kh99 05-22-2011 12:23 AM

I think you'd want

Code:

$sbmemgroups = $sbrow2[TABLE_PREFIX . 'user.membergroupids'];
or else just change the first line of your SQL to be

Code:

SELECT membergroupids

or I could be wrong. :)

ZERO <ibis> 05-22-2011 12:27 AM

Invalid SQL:

SELECT vbb_membergroupids
FROM vbb_user
WHERE vbb_user.userid = 1;

MySQL Error : Unknown column 'vbb_membergroupids' in 'field list'

This was the error for:

PHP Code:

$sbmemgroups $sbrow2[TABLE_PREFIX 'user.membergroupids']; 

The other option does nothing.

kh99 05-22-2011 12:28 AM

Try this:

PHP Code:

$sbquery2 $db->query_read("
            SELECT membergroupids
            FROM " 
TABLE_PREFIX "user
            WHERE userid = 
$userid
        "
);
        
        
$sbrow2 $db->fetch_array($sbquery2);
        
$sbmemgroups $sbrow2['membergroupids'];
    }

    
$sbusergroups explode(","$sbmemgroups); 


ZERO <ibis> 05-22-2011 12:32 AM

The above did nothing. No errors either.

--------------- Added [DATE]1306027999[/DATE] at [TIME]1306027999[/TIME] ---------------

For reference the following query runs fine:

PHP Code:

$sbquery $db->query_read("
            SELECT " 
TABLE_PREFIX "userfield.field9
            FROM " 
TABLE_PREFIX "userfield
            WHERE " 
TABLE_PREFIX "userfield.userid = $userid
        "
);

        
$sbrow $db->fetch_array($sbquery);
        
$sbaid $sbrow['field9']; 


kh99 05-22-2011 12:35 AM

Quote:

Originally Posted by ZERO <ibis> (Post 2198547)
For reference the following query runs fine:

OK, sorry, I guess i was wrong about what the problem was. Anyone else?

ZERO <ibis> 05-22-2011 12:40 AM

Could this be the problem:

PHP Code:

foreach($sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

Once again the code will run fine it it is fed
PHP Code:

$userdata->fetch_field('membergroupids'

from useradmin_update_save which suggests that the problem is not in this part but in the sql of getting membergroupids directly from the database.

--------------- Added [DATE]1306036694[/DATE] at [TIME]1306036694[/TIME] ---------------

I have even tried:

Code:

$membergroupids = fetch_membergroupids_array($user, false);
still not working correctly... no errors I am making the changes directly in the function directly without using the hook to avoid any complications. I know the above function should be working as it is used as part of the default vb code within the same function.

but then doing:

PHP Code:

$sbusergroups $sbmemgroups;

foreach(
$sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

does nothing

however from useradmin_update_save I can do:

PHP Code:

$sbmemgroups $userdata->fetch_field('membergroupids');

$sbusergroups explode(","$sbmemgroups);

foreach(
$sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

and that works without any problem... all I want is to be able to get the member groups of the user in paidsub_build and process it though:
PHP Code:

foreach($sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

just as I am able to do from useradmin_update_save

--------------- Added [DATE]1306050752[/DATE] at [TIME]1306050752[/TIME] ---------------

I think there may be a way to get the output I want via the fetch_membergroupids_array function but I do not know how to use it.

As stated above as long as I can get something that works as the below does but for paidsub_buid I am good:
PHP Code:

$sbmemgroups $userdata->fetch_field('membergroupids');

$sbusergroups explode(","$sbmemgroups);

foreach(
$sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

--------------- Added [DATE]1306057439[/DATE] at [TIME]1306057439[/TIME] ---------------

Ok so here is what I did to test this out:

I fed the data directly from txt files so that I could see that the functions run correctly. The program grabs no data externally just runs identical code on the paidsub_build hook and the useradmin_update_save hook.

When run from the paidsub_build it never runs completely but useradmin_update_save works. They both use exactly the same code. Example:

PHP Code:

$sbmemgroups2 file_get_contents('data2.txt');
    
$sbusergroups explode(","$sbmemgroups2);

foreach(
$sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup);

if (
$sbgroupint == 16)
        {
            
$sbpaid true;
        }


The contents of data2.txt are not changed. When run via useradmin_update_save returns true and when run via paidsub_build returns false... :eek:

kh99 05-22-2011 01:34 PM

Is the code you posted the entire plugin? Are you sure the paidsub_build hook is even getting executed? Where are you checking the "returned" value?

The only thing I can think of from what you posted is that the paidsub_build hook is in a function and the other hook isn't, so if you're trying to set a global you probably need to add a "global $sbpaid;" line (or else set an existing global instead).

Also, sometimes you can debug by echoing an HTML comment, like

Code:

echo "<!-- sbusergroups = $sbusergroups -->\r\n"

then view the page source.

ZERO <ibis> 05-22-2011 05:27 PM

Oh I had it generating txt files that outputted the values being passed to ensure they were working. I even had ones for various parts of the functions so I know it runs fine until it gets into that loop and does the if statement checks. However I feed that loop with hard coded values of lets say 16 for both paidsub_build and useradmin_update_save but it does not work on paidsub_build and does work on useradmin_update_save

It is mind blowing. Even with the variables set to the same values how can I get different results from the same function where the only difference is that it is being run on a different hook.

The part where is breaks down is here:
PHP Code:

$sbusergroups explode(","$sbmemgroups2);

foreach(
$sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup); 

The above code works fine in useradmin_update_save but breaks on paidsub_build even when they are for a fact processing the exact same input.

I got a b-day and graduation parties to get to today so I will deal with this more tomorrow. Thanks for all the help so far :D

--------------- Added [DATE]1306090100[/DATE] at [TIME]1306090100[/TIME] ---------------

Here is a good chunk of the functions (slightly edited)

PHP Code:

function first($userid$findstuff true$sbaid ""$sbmemgroups "")
{
    global 
$db$vB_Groups$sB_Groups$sB_Names$sB_Server$sB_WebClan$sB_WebDefault$sB_WebUpper$sB_WebProbation$sB_WebRoot$sbaidfield;

    
$fp fopen('d_userid.txt''w');
        
fwrite($fp$userid);
        
fclose($fp);    
    
    
    if (
$findstuff)
    {
        
$sbquery $db->query_read("
            SELECT " 
TABLE_PREFIX "userfield.field9
            FROM " 
TABLE_PREFIX "userfield
            WHERE " 
TABLE_PREFIX "userfield.userid = $userid
        "
);

        
$sbrow $db->fetch_array($sbquery);
        
$sbaid $sbrow['field9'];
        
        
$sbquery2 $db->query_read("
            SELECT membergroupids
            FROM " 
TABLE_PREFIX "user
            WHERE userid = 
$userid
        "
); 
        
        
$sbrow2 $db->fetch_array($sbquery2);
        
$sbmemgroups $sbrow2['membergroupids']; 
        
        
$fp fopen('data.txt''w');
        
fwrite($fp$sbmemgroups);
        
fclose($fp);
        
    }    
    
    
function2($userid$sbaid$sbmemgroups);
}

function 
function2($userid$sbaid$sbmemgroups)
{
    global 
$db$vB_Groups$sB_Groups$sB_Names$sB_Server$sB_WebClan$sB_WebDefault$sB_WebUpper$sB_WebProbation$sB_WebRoot$sbaidfield;
    
    
$fp fopen('data2.txt''w');
        
fwrite($fp$sbmemgroups);
        
fclose($fp);    
        
    
$sbmemgroups2 file_get_contents('data2.txt');
    
    
$sbusergroups explode(","$sbmemgroups2);

    
$sbnever false;
    
$sbalways false;
    
$sbpaid false;

    
//Determin what admin type the user is
    
foreach($sbusergroups as $sbgroup)
    {
        
$sbgroupint intval($sbgroup);

        if (
$sbgroupint == 10)
        {
            
$sbnever true;
        }
        else if (
$sbgroupint == 9)
        {
            
$sbalways true;
        }
        else if (
$sbgroupint == 16)
        {
        
$fp fopen('d_should_have_admin.txt''w');
                
fwrite($fp$sbgroupint);
                
fclose($fp);
            
$sbpaid true;
        }
    }

    
//Assign privlages to the selected type
    
if (!$sbnever)
    {
        if (
$sbalways || $sbpaid)
        {
        
$fp fopen('d_has_admin.txt''w');
                
fwrite($fp$sbpaid);
                
fclose($fp); 

The last line shown here never gets run on paidsub_build nor does the d_should_have_admin.txt this is even if I just read contents directly drom data2.txt from when it ran via the other hook and this applies to all variables not just the $sbmemgroups2

Something goes wrong in that foreach loop and only does so in paidsub_build even when the loop is fed with the same input.

While I am new to programing in vbb I have coded for years in general html php sourcepawn java ect and never have I seen a situation where you can feed a function the same input and get different outputs where there was not complex math or randomization involved... these results are blowing my mind and the only think I can think is that the function that the paidsub_hook is running in some how forcefully prevents the running of that loop. However, that would mean that we need to get a developer or someone that really understands the vb code in and out to explain just what is going wrong here.

Boofo 05-22-2011 05:59 PM

First, you only use the TABLE_PREFIX for tables and not fields or columns (as in Kevin's post above which is proper).

ZERO <ibis> 05-22-2011 06:08 PM

I will make that change when I can but I can say it is not what is going wrong as that still outputs 6 as it should. I even bypassed it completely by just hardcoading $sbaid=6; in its place for a few runs. (as I make a post before jumping in the shower, I am actually writing post bys as I get ready for the day and try to get all this info posted up lol)

Thanks again for your help everyone, hopefully this can be figured out once I try out some new changes tomorrow.

kh99 05-23-2011 01:10 AM

FWIW I put this code in a plugin on both hooks and it worked in both places, both with and without 16 in the list of groups in data2.txt.

PHP Code:

$sbmemgroups2 file_get_contents('data2.txt'); 
     
$sbusergroups explode(","$sbmemgroups2); 

foreach(
$sbusergroups as $sbgroup
    { 
        
$sbgroupint intval($sbgroup); 

if (
$sbgroupint == 16
        { 
            
$sbpaid true
        } 
}

$fp fopen('output.txt'"w");
fwrite($fp"sbpaid = $sbpaid");
fclose($fp); 


ZERO <ibis> 05-23-2011 05:26 PM

Interesting that must mean that the issue is the variable that I was storing 16 in as that is the only difference. I will hard code in all numbers even for constant variables. In theory this should fix the problem.

--------------- Added [DATE]1306176320[/DATE] at [TIME]1306176320[/TIME] ---------------

Yea the issue is that when running in paidsub_build it randomly decides on its own if it is going to use constant variables or not.

I will now just hard code in all constant values in place of variables and hopefully that will then work.

--------------- Added [DATE]1306176915[/DATE] at [TIME]1306176915[/TIME] ---------------

Ok it appears to be working now. The issue is that for what ever reason within the paidsub_build hook you can not use any global variables other than $db so anything you want to use variable wise must be defined within the function. This is only the case with the paidsub_ hooks.

I now can move on to programing the rest which should be easy now that it is clear what the rules are for these functions.

Thank you all so much for you help, I could not have gotten this working without you!

kh99 05-23-2011 06:17 PM

Glad you got it working. Yeah, the paidsub_build hook is in a function, so any globals you want to use would have to be declared with a "global" statement (except any vbulletin vars which might already be declared global in that function).

Edit: Oh, yeah, I do see you have the big list of globals in the code you posted a while back. Weird.

ZERO <ibis> 05-23-2011 06:50 PM

Oh they were declared with a global statement. They just did not work despite that but worked fine in other hooks...

Just a very strange glitch I never expected variables that were declared in a global statement to work on one hook but not on another. But whatever all I care about is the dam thing works now lol


All times are GMT. The time now is 09:13 AM.

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.01290 seconds
  • Memory Usage 1,872KB
  • 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
  • (4)bbcode_code_printable
  • (17)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (17)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