Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 01-27-2010, 08:33 PM
annatar annatar is offline
 
Join Date: Jan 2010
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default db insert failure in new mod

Greetings all.

I'm trying my hand at writing a vBulletin mod for the first time. So far things are going fairly well. I've been able to construct a sample product xml which properly creates and removes a new table to the forum database on install/uninstall.

I've created a plugin which uses the attachment_complete hook. I can see that the plugin is being called after an attachment is viewed for but some reason a database error is occurring. When I look at the query as listed in the error report that I am receiving via email it looks proper and I can in fact execute the query in phpmyadmin successfully.

The plugin I am trying to write simply records which user id downloaded which attachment id and from what host.

The plugin code is:

Code:
global $vbulletin, $db;

$userId = $vbulletin->userinfo['userid'];
$attachId = $_REQUEST['attachmentid'];
$sessionId = $vbulletin->session->vars['host'];

$db->query_write("
    insert into " . TABLE_PREFIX ."attachmentDownloadHistory 
        (userid, host, attachmentid)
    values
        ($userId, '$sessionId', $attachId)
");
$db->show_errors();
In the email report the error is

Code:
Invalid SQL:

    insert into testforum_attachmentDownloadHistory 
        (userid, host, attachmentid)
    values
        (1, '127.0.0.1', 2);
The error report lists the MySQL Error and Error Number field as both blank.

Any thoughts, suggestions or steps I might have overlooked?
Reply With Quote
  #2  
Old 01-27-2010, 08:48 PM
sheppardzwc sheppardzwc is offline
 
Join Date: Dec 2008
Location: South Carolina
Posts: 104
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

All MySQL statements that involve INSERT have to have all the variables/strings encased with an apostrophe. In basic terms, the correct statement would be:

PHP Code:
$db->query_write("INSERT INTO " TABLE_PREFIX "attachmentDownloadHistory (userid, host, attachmentid) VALUES ('$userId', '$sessionId', '$attachId')"); 

So basically, each variable after VALUES needs to have an apostrophe around it.

As well, you can also just use this for the queries instead of $db->, to cut down on processing time for PHP with "global:"

PHP Code:
global $vbulletin;
$vbulletin->db->query_write(whatever); 

Reply With Quote
  #3  
Old 01-27-2010, 09:03 PM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I don't know very much about it, but (if the above suggestions don't work) did you try moving the "insert" up to the previous line? Maybe it doesn't like having a newline before any other input. But that's just a guess.
Reply With Quote
  #4  
Old 01-27-2010, 11:30 PM
annatar annatar is offline
 
Join Date: Jan 2010
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks for the responses but quoting the variables with the parameters did not resolve the behavior I'm seeing.

It's a bother that the database error report doesn't include a MySQL Error or Error Number.

In the error report I did notice this:

Warning: mysql_errno(): 7 is not a valid MySQL-Link resource in /includes/class_core.php on line 408

Any ideas on that? It's only a warning but it doesn't sound good.
Reply With Quote
  #5  
Old 01-28-2010, 02:35 AM
kh99 kh99 is offline
 
Join Date: Aug 2009
Location: Maine
Posts: 13,185
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I think I might know - looking at attachment.php, there are conditions where exec_shut_down (in includes/functions.php) or $db->close() is called before the attachment_complete hook is executed. Maybe you can use the attachment_display hook instead? It's called earlier in attachment.php, but it looks like it's called under the same conditions.
Reply With Quote
  #6  
Old 01-28-2010, 04:16 AM
annatar annatar is offline
 
Join Date: Jan 2010
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Ah, that was a very handy suggestion. :up:

When I changed the hook to attachment_display I started to see records inserted into my database table. However it was a small surprise to me to see that when thumbnails are display that hook is triggered. I had to make a small change to my code to skip over thumbnails since I'm not interested in tracking that data at the moment. The revised code below seems to now work.

As an aside I was able to remove the extra quoting of all the query parameters that I'd added as suggested above. Since my table schema defines userid and attachmentid as int(10) the quoting doesn't appear to be required. It did work perfectly fine with them in place but it seems extraneous.

Code:
global $vbulletin;

$isThumb = $vbulletin->GPC['thumb'];
if (!$isThumb)
{
   $userId = $vbulletin->userinfo['userid'];
   $attachId = $vbulletin->GPC['attachmentid'];

   $sessionId = $vbulletin->session->vars['host'];

   $vbulletin->db->query_write("
       insert into " . TABLE_PREFIX ."attachmentDownloadHistory 
           (userid, host, attachmentid)
       values
           ($userId, '$sessionId', $attachId)
   ");
   $vbulletin->db->show_errors();
}
Reply With Quote
  #7  
Old 02-01-2010, 12:29 PM
Marco van Herwaarden Marco van Herwaarden is offline
 
Join Date: Jul 2004
Posts: 25,415
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by sheppardzwc View Post
All MySQL statements that involve INSERT have to have all the variables/strings encased with an apostrophe. In basic terms, the correct statement would be:

PHP Code:
$db->query_write("INSERT INTO " TABLE_PREFIX "attachmentDownloadHistory (userid, host, attachmentid) VALUES ('$userId', '$sessionId', '$attachId')"); 

So basically, each variable after VALUES needs to have an apostrophe around it.

As well, you can also just use this for the queries instead of $db->, to cut down on processing time for PHP with "global:"

PHP Code:
global $vbulletin;
$vbulletin->db->query_write(whatever); 

This is not correct, numeric columns do not need to be quoted, it is even against the coding standards to do so.
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:22 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.03859 seconds
  • Memory Usage 2,231KB
  • Queries Executed 13 (?)
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
  • (3)bbcode_code
  • (4)bbcode_php
  • (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
  • (7)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (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_postinfo_query
  • fetch_postinfo
  • 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
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete