Aug 1, 2012

Php Mysql blog tutorial, creating a simple and secured blog

php mysql blog

On today's tutorial we will talk about creating a blog on php and mysql

Table of contest:
1. Connecting to mysql database
2. Creating mysql tables
3. Coding the blog

              Download

Connecting to mysql database is simple, in just few lines of code

Before we start i suggest you read Php Mysql posting to database for better understanding of mysql.

<?php
// connecting to host
mysql_connect("host", "user", "password"); // replace this with your data
mysql_select_db("your_database"); // database name here
?>


Creating database, tables and columns
Now let's build the mysql table.
First build a database if you haven't, and dont forget to edit the connection details at the top.
Click on databases
php mysql blog
Set a name to it and click create.
Now we must build the tables:
mysql create tables
Click 'go', a new table will popup, complete those fields like in the pocture below:
Scroll down and click save.
Last one, click on posts and insert tab at top, write something to title, and id live empty.

Now lets continue coding the blog

<?php
// connecting to host
mysql_connect("host", "user", "password"); // replace this with your data
mysql_select_db("your_database"); // database name here

// lets build a blog
if(isset($_GET['post'])){
$post = $_GET['post'];

// first of all we will check string length
if(strlen($post) > 11){ // if post id is bigger than 11 charachters
die('Blog post nof found.');
}
// now we will make sure that the post id is numeric and this is a nice security method
if(is_numeric($post)){ // is numeric allows numbers only
$post = (int)$post; // and the int function, which replace every
// string to its correspoing number

// for the tutorial im gonna add mysql_real_escape_string
// but is not really needed in this case
$post = mysql_real_escape_string($post); // final sqli defense

// final part
$query = mysql_query("SELECT title FROM posts WHERE id=$post LIMIT 1");
while($row = mysql_fetch_array($query)){
echo $row['title'];
}

}
else{ // if post is not numeric then
die('Blog post nof found.'); // post does not exist
}

}
else{
// if post is not submitted display them all
$query = mysql_query("SELECT title, id FROM posts");
while($row = mysql_fetch_array($query)){
echo $row['title'];
echo $row['id'];
echo '<a href="?post='.$id.'">'.$title.'</a><br>';
}
}
?>
Few last tips!

I used LIMIT 1 on the first mysql query.
That's done for 2 main pruposes:
1. we are only displaying 1 result
2. it's a nice speed optinization for mysql
To display blog posts by newest ones replace


SELECT title, id FROM posts
with
SELECT title, id FROM posts ORDER BY id DESC
It's a simple blog who does the work.
If you want to learn more about sql injection we suggest Sql Injection prevention tutorial

78 comments:

  1. Excellent blog very nice and unique information related to Php Mysql blog tutorial. Thanks for sharing this information.

    SEO courses

    ReplyDelete
  2. Was having some trouble understand how to pull the information from the DB and show its result, but after reading your tutorial i have a complete understanding of it now, great commenting. Thanks!

    ReplyDelete
    Replies
    1. Creating Experts

      php Training in chennai

      Creating Experts provide Real Time scenarios php training in Chennai.At creating experts the trainers have around 8+ years of experience.They teach the advance level topics by giving real time examples.

      Contact us: 8122241286, 9003085882 – Karthick

      Here everything is practical basis not any classroom training.The trainers are working in mnc's and they will take classes only on Saturday and Sunday.

      Contact us : 8122241286, 9003085882 – Karthick


      Ref Link: http://thecreatingexperts.com/php-training-in-chennai/

      Delete
  3. Excelente el blog tanto la info como el diseño y la estetica del mismo, lo estoy poniendo entre mis favoritos, saludos cordiales.-

    ReplyDelete
  4. Hi there. PHP mysql is the growing trend now. It is a quite nice post PHP professionals. Thanks for sharing it.

    ReplyDelete
  5. Awesome post for Freshers and PHP professionals those who are in search of job or someone working in Non IT sector and desire to extend their skill sets to be a PHP Web Developer.
    Thanks for sharing appropriate info.

    ReplyDelete
  6. wow now i understood it very well. thank you

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. I am really glad I've found this information. Nowadays bloggers publish only about gossips and net and this is really irritating. A good blog with interesting content, this is what I need. Thank you

    ReplyDelete
  9. HI, for some reason i get the following error. Can you help please|
    ( ! ) Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wampXXXXXXXXXXXXXXXXXXXX (deleted sorry!)
    Call Stack
    # Time Memory Function Location
    1 0.0015 371600 {main}( ) ..\newblog08052013.php:0
    2 0.0364 388776 mysql_fetch_array ( ) ..\newblog08052013.php:37

    ReplyDelete
  10. that was great tnx very much :)))))))))

    ReplyDelete
  11. Thanks a lot.Through this post I learned lot of new things to create my new blog.

    ReplyDelete
  12. I can see that you are are genuinely passionate about this! I am trying to build my own website and youve helped me with some great
    information.
    http://www.sqlservermasters.com/

    ReplyDelete
  13. Replies
    1. can you tell me where to find mysql 5.6 developer(1Z0-882) exam in PDF/VCE??

      Thanks:D

      Delete
  14. The tutorial is best suited for Freshers who are in search of job or someone working in Non IT sector and desire to extend their skill sets to be a PHP Web Developer. This tutorial is also suited for people who want to migrate from other scripting languages like asp.net, cold fusion, perl, jsp to the rewarding world of PHP 5 and Zend Framework.
    Thanks for sharing.

    Learnpact

    ReplyDelete
  15. pls provide...platforms for others to blog also

    ReplyDelete
  16. Nice post. I usually use PDO to avoid SQL injection.

    ReplyDelete
  17. Thank for sharing nice tutorial of creating blog using php and Mysql. It is very helpful to us.

    ReplyDelete
  18. Hi..i appreciates your blog post, your detailed specification and steps are very helpful for us to make our own blog at free of cost. Thanks for haring this features with us.

    ReplyDelete
  19. Nice blog post! But if you are newbie to mysql then you should learn Basics of MySQL and PHP communication in detail. Thank you.

    Kind Regards,
    Sahil Sharma
    www.webdevelopersahil.com

    ReplyDelete
  20. hi i have the actual problem, when i click to a post , give me this error Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/CCCCC/public_html/index.php on line 84 Line 84 is // final part
    $query = mysql_query("SELECT title, FROM posts WHERE id=$post LIMIT 1");
    while($row = mysql_fetch_array($query)){
    echo $row['title'];
    }
    please help!

    ReplyDelete
  21. it is really a worthy one and all the points in the above blog were very useful, freshers for blogging would find it useful and really it would be useful for them.
    thank you

    with regards,
    krishna

    ReplyDelete
  22. Good information for me. please keep updating us with new posts. thank you so much .

    ReplyDelete
  23. This blog is very informative for me please keep it up. Empowerment Technology

    ReplyDelete
  24. for more programming tutorial visit http://www.9code.in

    ReplyDelete
  25. Its one of my favorite PHP tutorial blogs and more insightful information.It fulfill my needs too.

    ReplyDelete
  26. thanks for sharing an awesome php tutorial for all beginners..

    ReplyDelete
  27. Its really helpful for me to understand where we i lost in my previous interview. Thanks.
    If anyone wants to Learn oracle in Chennai go to the Besant Technologies which is No.1 Training Institute in Chennai
    http://www.oracletraininginchennai.in

    ReplyDelete
  28. I got a job by saying this answer in my last interview. thanks for awesome help.
    I got more idea about Oracle from Besant Technologies. If anyone wants to get oracle Training in Chennai visit Besant Technologies.

    http://www.oracletraininginchennai.in

    ReplyDelete
  29. http://matrixnmedia-staging.com/common/fb_responsive/

    ReplyDelete
  30. Using PDO in PHP on Mysql coding makes itsecured.

    ReplyDelete
  31. Being a novice in coding how can I implement this one in one simple html website? Can anyone point me some realistic directions?

    ReplyDelete
  32. Great Post... Hope it would be helpful for Web Developer..
    webline infosoft - Dehradun is also helpful for designing and development the website.

    ReplyDelete
  33. such a nice and useful information, i request to please keep it up this blog

    ReplyDelete
  34. Good post Mikel, it's always good to see developers helping others and by the looks of it lots of people have found this really useful. Thanks for sharing :)

    ReplyDelete
  35. I am searching an article for like this,finally I reached your blog. I feel this is a Very good post! Keep up the great writing.
    Online Booking system

    ReplyDelete
  36. I have read your Article very useful for python programming.They are site is very useful clearing the errors of python programming.Thank you for sharing you information.Python Training in Chennai

    ReplyDelete
  37. Thanks for this great post! It has been very helpful for my site zachariahlarsen.com

    ReplyDelete
  38. I like your blog post and helpful for me make connection thanks for share How to connect MySQL Database in Php with easy Steps

    ReplyDelete
  39. Hello,
    if you guyz need to create some basic validation and logics to process a long html form,check out this.. www.kodebuilder.com

    ReplyDelete
  40. Replies
    1. I can't send my answer to the MySQLexam, Why??

      Delete
  41. I am so happy to read this. This is the kind of manual that needs to be given and not the random misinformation that's at the other blogs.
    Starbucks Story of Growth Case Solution

    ReplyDelete
  42. Really this is very informative blog if anyone looking for learn php then we provided PHP Tutorial this is free online tutorial

    ReplyDelete
  43. Free Online Classified | Post Ad | US Classifieds - mukru.com usa no. 1 largest marketplace to buy, sell, find used product, servicees in your local area.
    http://www.mukru.com

    ReplyDelete
  44. This article is really awesome. It enjoys me very much. The content are so much informative.Thank you writer for giving me such a good article. SeoDevil15

    ReplyDelete
  45. You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!.Seo BaclinksSocial Bookmarking And Backlinks Service.

    ReplyDelete
  46. Hire PHP developers from a skilled and experienced PHP web development company which provides excellent PHP development services.

    ReplyDelete
  47. Creating Experts

    php Training in chennai

    Creating Experts provide Real Time scenarios php training in Chennai.At creating experts the trainers have around 8+ years of experience.They teach the advance level topics by giving real time examples.

    Contact us: 8122241286, 9003085882 – Karthick

    Here everything is practical basis not any classroom training.The trainers are working in mnc's and they will take classes only on Saturday and Sunday.

    Contact us : 8122241286, 9003085882 – Karthick


    Ref Link: http://thecreatingexperts.com/php-training-in-chennai/

    ReplyDelete
  48. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about php

    ReplyDelete
  49. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about
    php

    ReplyDelete
  50. This blog is not only very interesting but also very informative. i never knew that bloggers do have a task to handle, please do keep up with the great work.
    resume writing services

    ReplyDelete
  51. Great discussion and inform on where is web design going in 2016. Each and every year web design is growing and plenty of innovative and impressive things are shared and published every day.e-commerce web designs

    ReplyDelete
  52. Wonderful article. This will be really useful for beginners, and I like how you've lucidly explained the flow with screenshots. On behalf of http://phpprojects.org, thanks!

    ReplyDelete
  53. Pls i need a blog for my set of Novices brothers... but im struggling to create one... i don't need a host.. i need a simple but good and free one... i would really appreciate you help me create one.. you could create a new email and link to my email androvishberrian@gmail.com

    ReplyDelete
  54. Uhmmm... some info are: Don Formation House Ondo - Nigeria... the name for the blog sdbdream... thanks

    ReplyDelete