Author Topic: Best way to store blog posts  (Read 11272 times)

0 Members and 1 Guest are viewing this topic.

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Best way to store blog posts
« on: February 18, 2014, 03:48:04 am »
Hi everyone! I'm making my own blog framework, and I'd like to know the best way to store post's content. I planned for storing the content in files because I plan to support Markdown editing then updating the resulting php, but won't that be faster in a SQL Database? (storing the MArkdown sources externally then storing the result in a DB?)

Thanks in Advance.
Post not working but the style is defined.

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Best way to store blog posts
« Reply #1 on: February 18, 2014, 04:45:43 pm »
Well, I would go for mySQL as it is easy to implement with PHP and mySQL is fun.

But then again, i wrote for withg a blog which uses system-msg as a backend which uses markdown, it can be found here: http://withg.org/blog/

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Juju

  • Incredibly sexy mare
  • Coder Of Tomorrow
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 5730
  • Rating: +500/-19
  • Weird programmer
    • View Profile
    • juju2143's shed
Re: Best way to store blog posts
« Reply #2 on: February 18, 2014, 05:38:15 pm »
Either MySQL/MariaDB or sqlite will probably do.

Remember the day the walrus started to fly...

I finally cleared my sig after 4 years you're happy now?
THEGAME
This signature is ridiculously large you've been warned.

The cute mare that used to be in my avatar is Yuki Kagayaki, you can follow her on Facebook and Tumblr.

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
Re: Best way to store blog posts
« Reply #3 on: February 18, 2014, 06:00:49 pm »
I personally use filesystems for storing articles, but I use mysql for all other things. This is mainly because my own framework was made back when I didn't known mysql. If I rewrote it from scratch I would definitely put everything in the database. No messing around with htaccess files nor directory whitelisting. Mysql works great with php.
If you like my work: why not give me an internet?








Offline Eeems

  • Mr. Dictator
  • Administrator
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6266
  • Rating: +318/-36
  • little oof
    • View Profile
    • Eeems
Re: Best way to store blog posts
« Reply #4 on: February 18, 2014, 06:17:48 pm »
Well, I would go for mySQL as it is easy to implement with PHP and mySQL is fun.

But then again, i wrote for withg a blog which uses system-msg as a backend which uses markdown, it can be found here: http://withg.org/blog/
Well, system-msg already stored to flat files, you just wrote a web interface for it instead of using it as a backend.
/e

Offline Lunar Fire

  • LV3 Member (Next: 100)
  • ***
  • Posts: 66
  • Rating: +7/-1
  • I'll be watching you from the shadows
    • View Profile
    • My Tumblr
Re: Best way to store blog posts
« Reply #5 on: February 18, 2014, 06:45:46 pm »
I really suggest storing them in your database. The Blob type can contain just about anything, and it's much easier to manage your permissions in the database than using the filesystem.

And considering it's a blog post, it should be stored in a markup language (HTML, BBCode, etc.) so it will probably be better to store it in a text-based type rather than a blob.
Your drill is the drill that will pierce the heavens!

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Best way to store blog posts
« Reply #6 on: February 19, 2014, 02:45:26 am »
Well, I would go for mySQL as it is easy to implement with PHP and mySQL is fun.

But then again, i wrote for withg a blog which uses system-msg as a backend which uses markdown, it can be found here: http://withg.org/blog/
Well, system-msg already stored to flat files, you just wrote a web interface for it instead of using it as a backend.
Yeah, if it wouldn't already exist as files, i would've have probably gone for database.

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Best way to store blog posts
« Reply #7 on: February 19, 2014, 02:47:32 am »
I'm already using a DB. I just use it to link to the files.

How do you store/get the blob with Maria/MySQL?

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Best way to store blog posts
« Reply #8 on: February 19, 2014, 02:52:30 am »
well, storing would be then like
Code: [Select]
INSERT INTO `blog_posts` (post,whateverOtherColumnsYouWantToSet) VALUES ('%s','%s')
Well, substituting %s  with the corresponding value of cource

Updating would be like this:
Code: [Select]
UPDATE `blog_posts` SET post='%s' WHERE postId=%d
If you want to update more columns, just add them with a comman, like this:
post='%s',name='5s'

And to fetch:
Code: [Select]
SELECT post,name,whatever FROM `blog_posts` WHERE postId`%d


I hope that quick guide helped a tad :)

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Best way to store blog posts
« Reply #9 on: February 19, 2014, 03:19:46 am »
It seems quite strange to use a DB for storing things but why not. Thanks.

EDIT : Now I store the posts in a value directly in the DB, thanks for your advice. I need to implement now a back-end interface.
(UPDATE SET text=$string WHERE id = $id);
« Last Edit: February 19, 2014, 04:26:30 am by Eiyeron »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Best way to store blog posts
« Reply #10 on: February 19, 2014, 06:05:35 am »
nonononono, NEVER put php variables directly into a query, evil things can be done then easilly, like SQL injection and stuff like that.

Allow me to copy-pasta you a function i wrote that does all that escaping stuff automatically:
Code: [Select]
class Sql{
private $mysqliConnection;
private $queryNum;
public function __construct(){
$this->queryNum = 0;
}
private function connectSql(){
if(!isset($this->mysqliConnection)){ //if no connection yet we create one
$this->mysqliConnection = new mysqli('localhost','sql_user','sql_password','sql_database');
if ($this->mysqliConnection->connect_errno)
die('Could not connect to SQL DB: '.$this->mysqliConnection->connect_errno.' '.$this->mysqliConnection->connect_error);
$this->mysqliConnection->autocommit(true);
}
}
public function query($query,$args = [],$num = false){
$this->connectSql(); //connect to sql
for($i=0;$i<count($args);$i++) //escape arguments
$args[$i] = $this->mysqliConnection->real_escape_string($args[$i]);
$result = $this->mysqliConnection->query(vsprintf($query,$args)); //insert arguments in query and execute it
$this->queryNum++; //add one to the query counter
if($this->mysqliConnection->errno==1065) //empty
return array();
if($this->mysqliConnection->errno!=0)
die($this->mysqliConnection->error.' Query: '.vsprintf($query,$args));
if($result===true) //nothing returned
return array();
$res = array();
$i = 0;
while($row = $result->fetch_assoc()){
$res[] = $row;
if($num!==false && $i===$num){ //if the user set an element then we only reaturn that one
$result->free();
return $row;
}
if($i++>=150) //we don't want php to mem overflow
break;
}
if($res === []){ //if the result is empty we create a dummy array with the assoc fields, each containing NULL
$fields = $result->fetch_fields();
for($i=0;$i<count($fields);$i++) //create each field and set it to NULL
$res[$fields[$i]->name] = NULL;
if($num===false) //if we didn't set an element we expect it to be an array in an array
$res = array($res);
}
$result->free();
return $res; //return the result
}
}
$sql = new Sql();
Ok, this has also some features you may actually won't need, lol.
The key is to do a mysqli_real_escape_string() on all your data before putting it in, and be sure, that if you enter a string in SQL you put single quotes around it.
Example usage:
Code: [Select]
$res = $sql->query("SELECT * FROM `blog_posts` WHERE id=%d",[$number]);
$res will contain now an array with all results, but, as in this case, you only need the first result, I would do something like:
Code: [Select]
$res = $sql->query("SELECT * FROM `blog_posts` WHERE id=%d",[$number],0);
The 0 tells it to only grab the first result.
If the result is empty that function will still create array stuff, but put NULL everywhere in.

Oh, another example for string stuff:
Code: [Select]
$sql->query("INSERT INTO `blog_posts` (post,name) VALUES ('%s','%s')",[$post,$name]);

Ok, maybe that was a bit overkill now, you can write your own functions too, just make sure you _real_escape_string() everything you put in and use single quotes around strings! And if you put in an int make sure it IS an int, you can do that by pre-fixing the php variables with (int), example: (int)$id


THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Best way to store blog posts
« Reply #11 on: February 19, 2014, 09:05:54 am »
I use prepared queries with PDO! :D

But your function seems to be inetresseting, I should read it later to understand it better.
« Last Edit: February 19, 2014, 09:08:44 am by Eiyeron »

Offline Sorunome

  • Fox Fox Fox Fox Fox Fox Fox!
  • Support Staff
  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 7920
  • Rating: +374/-13
  • Derpy Hooves
    • View Profile
    • My website! (You might lose the game)
Re: Best way to store blog posts
« Reply #12 on: February 19, 2014, 09:06:45 am »
oh, ok, nice! But i would still use the single quotes on strings :P

THE GAME
Also, check out my website
If OmnomIRC is screwed up, blame me!
Click here to give me an internet!

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Re: Best way to store blog posts
« Reply #13 on: February 19, 2014, 09:09:05 am »
oh, ok, nice! But i would still use the single quotes on strings :P

Edited after you replied --"

Offline Eiyeron

  • Urist McEiyolobster
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1430
  • Rating: +130/-10
  • (-_(//));
    • View Profile
    • Rétro-Actif : Rétro/Prog/Blog
Best way to manage sessions
« Reply #14 on: February 25, 2014, 02:18:11 am »
Hi back!

Currently trying to implement correctly back-side, I need to support sessions. How do I manage them the best way? Don't remember my lessons... --"