Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 12-30-2009, 04:13 AM
Warlord's Avatar
Warlord Warlord is offline
 
Join Date: Jan 2002
Location: TN, USA
Posts: 668
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Inserting SQL in PHP Problem - please help

I'm trying to insert the following code in a hook but I have a syntax error and I can't seem to figure out how to make it work.

Code:
	function prepare_output($id = '', $options = array())
	{
		$this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
	}
}
Here is the error I'm generating:


Parse error: syntax error, unexpected T_VARIABLE in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 247


I'm pretty sure it's the usage of the double and single quotes in my query but I don't know how else to call for the information without it. Can anyone help me please?

Here is the entire plug in code below:

Code:
$blocklist = array_merge($blocklist, array(
	'longbox' => array(
		'class' => 'Longbox',
		'title' => 'Resume',
		'hook_location' => 'profile_left_last'
	)
));

class vB_ProfileBlock_Longbox extends vB_ProfileBlock
{
	var $template_name = 'memberinfo_block_longboxes';

	function confirm_empty_wrap()
	{
		return false;
	}

	function confirm_display()
	{
		return ($this->block_data['longbox'] != '');
	}

	function prepare_output($id = '', $options = array())
	{
		$this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
	}
}
Reply With Quote
  #2  
Old 12-30-2009, 04:54 AM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Code:
    function prepare_output($id = '', $options = array())
    {
        $this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
    }
should be
Code:
    function prepare_output($id = '', $options = array())
    {
        $this->block_data['longbox'] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ");
    }
Needed to remove the extra ' ... ' around the $db->query_first() function call. That is supposed to be PHP - not a string. The query itself is already quoted with the double quotes.

Cheers
Reply With Quote
  #3  
Old 12-30-2009, 02:26 PM
Warlord's Avatar
Warlord Warlord is offline
 
Join Date: Jan 2002
Location: TN, USA
Posts: 668
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by SirAdrian View Post
Code:
    function prepare_output($id = '', $options = array())
    {
        $this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
    }
should be
Code:
    function prepare_output($id = '', $options = array())
    {
        $this->block_data['longbox'] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ");
    }
Needed to remove the extra ' ... ' around the $db->query_first() function call. That is supposed to be PHP - not a string. The query itself is already quoted with the double quotes.

Cheers
I tried that before and I freaked out the first time I did it that way because the profile stopped working at all and I got a white screen with the below error. :

Fatal error: Call to a member function query_first() on a non-object in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 247

I didn't realize it until now but I think that the error above is actually a step in the right direction, because it's telling me that I need to fix something else. I did some googling on that error and from what I can tell I need to globalise the variable $db->query_first by adding global $db->query_first; in my code.

After adding it to the top of the plugin my profile page loads but has the following error and now I feel like I'm back at step 1.

Parse error: syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';' in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 223.

Here's what my plugin looks like now.

Code:
global $db->query_first;

$blocklist = array_merge($blocklist, array(
	'longbox' => array(
		'class' => 'Longbox',
		'title' => 'Resume',
		'hook_location' => 'profile_left_last'
	)
));

class vB_ProfileBlock_Longbox extends vB_ProfileBlock
{
	var $template_name = 'memberinfo_block_longboxes';

	function confirm_empty_wrap()
	{
		return false;
	}

	function confirm_display()
	{
		return ($this->block_data['longbox'] != '');
	}

    function prepare_output($id = '', $options = array())
    {
        $this->block_data['longbox'] = $db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    }
Reply With Quote
  #4  
Old 12-30-2009, 02:35 PM
DragonBlade's Avatar
DragonBlade DragonBlade is offline
 
Join Date: May 2006
Posts: 189
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Your $db variable ain't global. XP

Before that line, insert:
PHP Code:
global $db
---leave out the "query_first" bit, that's just a method of your object.

Not sure that this is your only error, but that's where you're having the trouble at the moment.

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

Basically, inside any function, if $db is not specifically declared as a global variable, it will only look at the variables LOCAL to the function.
Reply With Quote
  #5  
Old 12-30-2009, 02:55 PM
Warlord's Avatar
Warlord Warlord is offline
 
Join Date: Jan 2002
Location: TN, USA
Posts: 668
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by DragonBlade View Post
Your $db variable ain't global. XP

Before that line, insert:
PHP Code:
global $db
---leave out the "query_first" bit, that's just a method of your object.

Not sure that this is your only error, but that's where you're having the trouble at the moment.

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

Basically, inside any function, if $db is not specifically declared as a global variable, it will only look at the variables LOCAL to the function.
Ok I've removed the code to globalise the $db from the top of the plugin and added it to the prepare_output function, while dropping the query_first part, so now it looks like this:

Code:
function prepare_output($id = '', $options = array())

global $db;

    {
        $this->block_data['longbox'] = $db("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    }
Unfortunately it still seems to give me the same error:

Parse error: syntax error, unexpected T_GLOBAL, expecting ';' or '{' in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 247



EDIT: Oh wait, that's not the same error. To the interweb!
Reply With Quote
  #6  
Old 12-30-2009, 02:58 PM
bobster65's Avatar
bobster65 bobster65 is offline
 
Join Date: Mar 2006
Location: Montana
Posts: 1,169
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
function prepare_output($id ''$options = array())

global 
$db;

    {
        
$this->block_data['longbox'] = $db("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    } 
you have the global outside of the {}

should be like this..

PHP Code:
function prepare_output($id ''$options = array())

    {
        global 
$db;

        
$this->block_data['longbox'] = $db("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    } 
Reply With Quote
  #7  
Old 12-30-2009, 03:15 PM
Warlord's Avatar
Warlord Warlord is offline
 
Join Date: Jan 2002
Location: TN, USA
Posts: 668
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by bobster65 View Post
PHP Code:
function prepare_output($id ''$options = array())

global 
$db;

    {
        
$this->block_data['longbox'] = $db("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    } 
you have the global outside of the {}

should be like this..

PHP Code:
function prepare_output($id ''$options = array())

    {
        global 
$db;

        
$this->block_data['longbox'] = $db("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    } 
Yeah, I've been googling and you're right I needed to move it inside the curly bracket, but now I'm getting yet ANOTHER error. (I can't believe it either - it's like the coding gods hate me ).

Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 252
Reply With Quote
  #8  
Old 12-30-2009, 03:39 PM
DragonBlade's Avatar
DragonBlade DragonBlade is offline
 
Join Date: May 2006
Posts: 189
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Not that I'm not willing to help, but you might want to brush up on your PHP if you're not understanding these errors. XP

See the error? It ran into a "if" statement when it was expecting a "function" statement. Pretty straight-forward.

Now take a look at the line numbers--all of this is erring on line 463, inside of an eval'd code, see? That almost always means that it's a Plugin causing your woes, and if you look on line 463 in member.php, you will see that there's a hook there for plugins.

Now, the second number is "252". Notice that your previous error with misplacing the global was on line 247, so this new error is about 5 lines after that. This means that it's probably right at the end of this plugin OR it's right near the beginning of the next plugin that's loaded. (Without seeing the entire modifications you've made to this plugin, none of us can really tell you for certain, though.)

So post your complete edited code, and we'll try to help you out, but be sure to look at a few PHP Tutorials so you can get a better grasp on the PHP syntax. :3

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

Ah. >.>

PHP Code:
function prepare_output($id ''$options = array())

    {
        global 
$db;

        
$this->block_data['longbox'] = $db->query_first("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    } 
Heh. See, $db is a variable containing a Object, and query_first is a method of that Object. You are calling that Method with a string meant for MySQL, but you forgot to call the method. XP
Reply With Quote
  #9  
Old 12-30-2009, 04:27 PM
Warlord's Avatar
Warlord Warlord is offline
 
Join Date: Jan 2002
Location: TN, USA
Posts: 668
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

DragonBlade,

I appreciate the help, and as you've pointed out I do need to brush up on my PHP. Actually I've never taken any courses on PHP or anything, everything I have learned is for the most part through trial and error or trying to decipher other people's work. I didn't understand this latest error right away but I did figure out that it ran into an IF statement, I just can't figure out where.


Here is my member_block_resume template:

HTML Code:
<div class="alt1 block_row">
	<ul class="list_no_decoration">
		$block_data[resume]
	</ul>
</div>


Here is my plugin:
Hook Location: member_build_blocks_start
Title: Resume
Execution Order: 5
Plugin PHP Code:
Code:
$blocklist = array_merge($blocklist, array(
	'longbox' => array(
		'class' => 'resume',
		'title' => 'Resume',
		'hook_location' => 'profile_left_last'
	)
));

class vB_ProfileBlock_Longbox extends vB_ProfileBlock
{
	var $template_name = 'memberinfo_block_resume';

	function confirm_empty_wrap()
	{
		return false;
	}

	function confirm_display()
	{
		return ($this->block_data['resume'] != '');
	}

    function prepare_output($id = '', $options = array())

    {
        global $db;

        $this->block_data['resume'] = $db("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
    }
Quote:
Originally Posted by DragonBlade
Heh. See, $db is a variable containing a Object, and query_first is a method of that Object. You are calling that Method with a string meant for MySQL, but you forgot to call the method. XP
I tried looking up what you're talking about, because pretty much that entire paragraph confused me.

I know what variables are. I know what queries are. I didn't know what methods were (according to PHP.net they are the same thing as functions). And I still haven't found a definition of an object (I've found plenty of references to them, but not it's actual definition.)

So if $db is a variable containing an object, is an object just what that variable represents? For instance if the variable $a = 1, would 1 be the object?

Also, if methods and functions are the same thing, that means that the method/function in this case would be query_first which is a way to query the database, right? This kind of confused me because I thought I was supposed to drop query_first part like shown above.

The sentence that really threw me for a loop was.

Quote:
You are calling that Method with a string meant for MySQL, but you forgot to call the method.
I don't understand how I can call the method but forget to call the method. Is that a typo maybe, or am I just not understanding?
Reply With Quote
  #10  
Old 12-30-2009, 04:55 PM
DragonBlade's Avatar
DragonBlade DragonBlade is offline
 
Join Date: May 2006
Posts: 189
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hmmm... Let me see what the best way to define it is...


You probably know what Arrays are. Like $bbuserinfo--it's an array that holds multiple variables inside of it. It can even hold other Arrays and even Objects. $bbuserinfo has several "keys" that hold values: $bbuserinfo['userid'] holds the userid, $bbuserinfo['username'] the name, and so on.


Well, think of an Object as an Array that can hold it's own special functions (called "Methods").

$db is a Database Object that holds functions for communicating with the database.
$db->query_first() is a method that returns one row from the database.
$db->query_read() is a method that can return multiple rows.
$db->query_write() allows you to write to the database.

Objects can also hold regular variables;
$vbulletin is an Object, and
$vbulletin->userinfo is an array that holds the userinfo of the currently logged in user (like $bbuserinfo).
$vbulletin->userinfo['userid'] holds the UserID of the currently-logged-in-user.

Objects are really more of an advanced PHP topic that you really will only understand with experience, but they really make life a lot easier.




Okay, now to get to what it is you wanted, heh.

Add an end-curly-brace to the end of your code.


PHP Code:
$blocklist array_merge($blocklist, array(
    
'longbox' => array(
        
'class' => 'resume',
        
'title' => 'Resume',
        
'hook_location' => 'profile_left_last'
    
)
));

class 
vB_ProfileBlock_Longbox extends vB_ProfileBlock
{
  var 
$template_name 'memberinfo_block_resume';
  function 
confirm_empty_wrap()
  {
    return 
false;
  }
  function 
confirm_display()
  {
    return (
$this->block_data['resume'] != '');
  }
  function 
prepare_output($id ''$options = array())
  {
    global 
$db;
    
$this->block_data['resume'] = $db("SELECT * FROM " TABLE_PREFIX "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
  }

I don't really know what a great code editor is for Windows, which you're probably using, but I used to use ConTEXT (google "context text editor") and liked it. I use Kate on my Linux and I adore it, but I don't think they have it for Windows, but if you arrange your code nicely you can see that you were just missing an end bracket. XP



I'd like to help you out more on unerstanding Object, but I just can't think of a decent way to explain them without knowing just how far you've delved into PHP. XD But you seem very new to PHP, so really understanding Object might be a little beyond you at the moment. Still, if you want to take a looksee...

http://php.net/manual/en/language.oop5.php

Warning--you will NOT understand everything in one sitting; it's a very big topic to grasp.
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 11: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.04637 seconds
  • Memory Usage 2,314KB
  • 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
  • (9)bbcode_code
  • (1)bbcode_html
  • (8)bbcode_php
  • (5)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)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
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete