PDA

View Full Version : Inserting SQL in PHP Problem - please help


Warlord
12-30-2009, 04:13 AM
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.

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:

$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]' ")';
}
}

Adrian Schneider
12-30-2009, 04:54 AM
function prepare_output($id = '', $options = array())
{
$this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
}should be
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

Warlord
12-30-2009, 02:26 PM
function prepare_output($id = '', $options = array())
{
$this->block_data['longbox'] = '$db->query_first("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' ")';
}should be
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.

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' ");
}

DragonBlade
12-30-2009, 02:35 PM
Your $db variable ain't global. XP

Before that line, insert:
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 1262191036 at 1262191036 ---------------

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.

Warlord
12-30-2009, 02:55 PM
Your $db variable ain't global. XP

Before that line, insert:
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 1262191036 at 1262191036 ---------------

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:

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

:confused:

EDIT: Oh wait, that's not the same error. To the interweb! :D

bobster65
12-30-2009, 02:58 PM
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..

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' ");
}

Warlord
12-30-2009, 03:15 PM
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..

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 :p).

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

DragonBlade
12-30-2009, 03:39 PM
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 1262194924 at 1262194924 ---------------

Ah. >.>

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

Warlord
12-30-2009, 04:27 PM
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:

<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: $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' ");
}

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 (http://www.php.net/manual/en/language.oop5.basic.php) 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.

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?

DragonBlade
12-30-2009, 04:55 PM
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.


$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.

Warlord
12-30-2009, 08:27 PM
Thanks for the help, unfortunately every time I get one thing done something else is wrong. :(

Now it's telling me that the function name must be a string (on line 245).

I researched that error code and found this...

Well, I found the problem. It seems that I had a pair of parens ()' around the index of an array element instead of brackets []'s. Hence it was looking at a variable as a function. I.e.
Wrong: $arName('myindex')
Right: $arName['myindex']

http://www.webdeveloper.com/forum/showthread.php?t=81233

I figured maybe a variable in the array I was calling was being looked at as if it were a function like above, so I looked through the code and changed the parenthesis() around "SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' " to brackets[] but then I got this error.

Fatal error: Cannot use object of type vB_Database as array in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 245

But now that I look at it, I think that the parenthesis were right because even though it's part of the variable $db, it's still a sql query, right?

I think it's saying that I can't use the variable $db and I have to use a string instead for that part?

Anyway, I changed it back to parenthesis() and now I'm back to:

Fatal error: Function name must be a string in /home/righscom/public_html/addons/projectfanboy/vb/member.php(463) : eval()'d code on line 245

I'll keep researching. :(

--------------- Added 1262213487 at 1262213487 ---------------

Ok, I was re-reading your post and I think I was misunderstanding something before and it helps me to talk it out on "paper" so to speak.

Arrays look similar to variables.
Arrays are basically variables that hold multiple other variables (aka an array of other variables).

So, $db("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ") is not a variable but actually an array that uses a SQL query to generate its results.

But this is also an array, right?
array(
'class' => 'resume',
'title' => 'Resume',
'hook_location' => 'profile_left_last'
)

I think I may be starting to see the light.... (I'll probably edit this 15 more times while I look at it and think. :p)

--------------- Added 1262215159 at 1262215159 ---------------

Hmmm, I also found this...

There's also another problem that will cause the same type of response (function must be a string) and that's inadvertently including a $ in front of a predefined/language defined function.

So I tried removing the $ from in front of the "predefined function" $db but that gave me a different error so I changed it back. Still looking! :D

--------------- Added 1262215901 at 1262215901 ---------------

Ok, to see if this would work (it didn't but I'm trying to think outside the box :p)

I globalised $db and defined $resume as the sql query earlier in the code and then I defined $this->block_data['resume'] as $resume so the code looked like this.

{
global $db;
$resume = $db("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' , AND title = 'Resume' ");
}

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

class vB_ProfileBlock_resume 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())
{

$this->block_data['resume'] = $resume;
}
}

Like I said, it didn't work and I got the exact same error on a different line, but I get the feeling I may be on the right track?

--------------- Added 1262224021 at 1262224021 ---------------

Okay, here is the latest and greatest code for my plugin which seems to be working other than the fact that I don't think $bbuserinfo[userid] is parsing and so nothing shows up. But on a good note, I don't seem to be getting any errors anymore. :D

{
global $db;
$resume = $db->query_read("SELECT * FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' AND title = 'Resume' ");
}

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

class vB_ProfileBlock_resume 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())
{

$this->block_data['resume'] = $resume;
}
}

--------------- Added 1262224314 at 1262224314 ---------------

Well son of a mazza frazzer.... Just to test it out, I changed out $bbuserinfo[userid] for '162' (my userid) and nothing still showed up. :confused:

I really thought that would work....

--------------- Added 1262298398 at 1262298398 ---------------

Kind of at a point where I'm a bit stumped. :(

Warlord
01-02-2010, 04:59 AM
Any ideas?

Warlord
01-04-2010, 11:22 AM
I'm making headway! :D


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

class vB_ProfileBlock_resume 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;
$results = $db->query_read("SELECT output FROM " . TABLE_PREFIX . "formresults WHERE userid = '$bbuserinfo[userid]' AND title = 'Resume' ");

$this->block_data['resume'] = $results;
}
}


That get's no errors in the header, adds the Resume Tab to the profile and data is displayed in the content portion of the tab. The data it's displaying is confusing the heck out of me, but it's still a great leap forward for me I think. :D

Resource id #73 (the number changes with each member) is being generated in the tab's content portion. Still looking into this.