creating a plugin, how to connect to the database

Hey guys! this is my first post on vanilla forums since everythings changed.. Ive been a fan for about 6 years now and I wanted to start a site using the forums.
I'm working on a plugin that would allow me to edit pages - about and contact pages.
I created a database table for these 2 other pages, and I'm trying to edit them within the dashboard. i got the link to show up on the side, wrote all the script out (its varly small) and then when I went to try it, I found out I had to use the database classes..
I've been reading the docs -- http://vanillaforums.org/docs/database and I thought it would be easy to do by simply do the
$database = new Database;
but then its saying the database class isnt found.. I'm trying to call the class on a separate page within the dashboard.. can someone help me out? this is what I wrote out before I realized I needed to use the class..
$doIt = mysql_query("SELECT * FROM 'gdn_ExtraPages' WHERE id = '".mysql_real_escape_string($_POST['whichPage'])."' LIMIT 1") or die(mysql_error());
$gotIt = mysql_fetch_array($doIt);
mysql_free_result($doIt);
and $gotIt returns nothing..
If you could tell me what I need to do before that and how to get what I need from the database that would be awesome.. I'm almost thinking I need to create this within a class but not sure.. I'm gonna try that next..
Best Answer
-
Todd Vanilla Staff
You are getting the correct result there. Our Gdn_DataSet is a wrapper to deal with data from the database.
If you call
$get->ResultArray()
then you'll get an array of the actual data that should be asier for you to parse.0
Answers
I tried adding it to my class for this plugin like so
public function grabEP($id){
$grabIt = Gdn::SQL()->Select('*')->From('ExtraPages')->Where('id', $id)->Get();
return $grabIt;
}
called it like so..
$haha = $EPClass = new ExtraPagesPlugin;
$get = $haha->grabEP($_POST['whichPage']);
print_r($get);
and then got..
Gdn_DataSet Object
(
[Connection] => PDO Object
(
)
from GDN_ExtraPages ExtraPages
where id = :id
)
)
You are getting the correct result there. Our Gdn_DataSet is a wrapper to deal with data from the database.
If you call
$get->ResultArray()
then you'll get an array of the actual data that should be asier for you to parse.my friend wanted the website up fast and all I had left to do was a picture gallery page, so I ended up using text files to store the information.. I'm going to try to edit it up so it works with vanilla better and then submit it to the plugin directory..
thanks for the info!