How to add a field in the dataabse with install function for a plugin I am creating.
tonoshi Offline
Junior Member
**
Posts: 23
Joined: Aug 2010
Post: #1
How to add a field in the dataabse with install function for a plugin I am creating.
Hello. I am creating my first plugin ever for mybb and I have gotten everything to work with the exeption of adding a field in the forums table.

The query I am trying to add is this:

PHP Code:
UPDATE 'mybb_forums' ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'


When I run this query at the database my plugin works as it should. Howeevr I want to add it automatically with the install function in the plugin file. After I studied some plugins I tried this but it did not work.

PHP Code:
function forumsignore_install()
{
global $db;
$db->write_query("UPDATE ".TABLE_PREFIX."forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'");		
}


Can someone please show me what I am doing wrong and how to fix it?

Thank you Smile

08-04-2010 10:09 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #2
RE: How to add a field in the dataabse with install function for a plugin I am creating.
The MySQL query is incorrect.

You need to use an ALTER TABLE query to modify table schema.
http://dev.mysql.com/doc/refman/5.1/en/alter-table.html

UPDATE queries change values in the table, not the table itself.

Depending on what you do, you may also need to update the forum cache after you perform the query.  From memory, it should be something like:

PHP Code:
$cache->update_forums();


My Blog
(This post was last modified: 08-04-2010 10:29 PM by ZiNgA BuRgA.)
08-04-2010 10:28 PM
Find all posts by this user Quote this message in a reply
tonoshi Offline
Junior Member
**
Posts: 23
Joined: Aug 2010
Post: #3
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Thank you. I tried it but it is not working. This is the error message I get when I tried that.

PHP Code:
1
2
3
4
5
6
MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mybb_forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'' at line 1
Query:
    ALTER mybb_forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0' 



Can you please post the correct code for me with the right install and uninstall functions with the query? I would really appreciate it. I have been trying for quite some time and I havent found the solution.

08-04-2010 11:32 PM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #4
RE: How to add a field in the dataabse with install function for a plugin I am creating.
It's ALTER TABLE (look at the example queries on the page I linked to).

SQL Code
ALTER TABLE 'mybb_forums' ADD COLUMN 'forum_ignore' INT(1) NOT NULL DEFAULT '0'

ALTER TABLE 'mybb_forums' DROP COLUMN 'forum_ignore'


PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function forumsignore_install()
{
global $db, $cache;
$db->write_query("ALTER TABLE ".TABLE_PREFIX."forums ADD 'forum_ignore' INT(1) NOT NULL DEFAULT '0'");
$cache->update_forums();
}
function forumsignore_is_installed()
{
global $db;
return $db->field_exists('forum_ignore', 'forums');
}
function forumsignore_uninstall()
{
global $db, $cache;
$db->write_query("ALTER TABLE ".TABLE_PREFIX."forums DROP 'forum_ignore'");
$cache->update_forums();
}


My Blog
08-05-2010 08:12 AM
Find all posts by this user Quote this message in a reply
tonoshi Offline
Junior Member
**
Posts: 23
Joined: Aug 2010
Post: #5
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Thank you but it did not work for me. I got the same error again. I really appreciate your experienced help ZiNgA BuRgA.
08-06-2010 12:30 AM
Find all posts by this user Quote this message in a reply
RateU Offline
Administrator
*******
Posts: 2,350
Joined: Mar 2010
Post: #6
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Maybe Yumi meant something like this:

PHP Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function forumsignore_install()
{
global $db, $cache;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."forums` ADD `forum_ignore` INT(1) NOT NULL DEFAULT '0'");
$cache->update_forums();
}
function forumsignore_is_installed()
{
global $db;
return $db->field_exists('forum_ignore', 'forums');
}
function forumsignore_uninstall()
{
global $db, $cache;
$db->write_query("ALTER TABLE `".TABLE_PREFIX."forums` DROP `forum_ignore`");
$cache->update_forums();
}


08-06-2010 06:14 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #7
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Oh, I forgot that MySQL didn't like those quotes.  Sorry about that and thanks RateU!

My Blog
08-06-2010 06:25 PM
Find all posts by this user Quote this message in a reply
tonoshi Offline
Junior Member
**
Posts: 23
Joined: Aug 2010
Post: #8
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Thank you very much to both you. Now it works. I am still learning, was that quote difference that big that mysql didnt accept it?
08-07-2010 12:48 AM
Find all posts by this user Quote this message in a reply
ZiNgA BuRgA Offline
Fag
*******
Posts: 3,357
Joined: Jan 2008
Post: #9
RE: How to add a field in the dataabse with install function for a plugin I am creating.
I think that type of quote (single and double) is probably reserved for strings, not identifiers.

My Blog
08-07-2010 09:09 AM
Find all posts by this user Quote this message in a reply
tonoshi Offline
Junior Member
**
Posts: 23
Joined: Aug 2010
Post: #10
RE: How to add a field in the dataabse with install function for a plugin I am creating.
Thank you for your explanation.
08-09-2010 02:14 AM
Find all posts by this user Quote this message in a reply

« Next Oldest | Next Newest »

 Standard Tools
Forum Jump: