Home » , , » Php Mysql blog tutorial, creating a simple and secured blog

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

41 תגובות:

  1. Really like that you are providing such information on PHP MYSQl with JAVASCRIPT ,being enrolled at http://www.wiziq.com/course/5871-php-mysql-with-basic-javascript-integrated-course i really thank you for providing such information it was helpful.

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

    SEO courses

    ReplyDelete
  3. 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
  4. Excelente el blog tanto la info como el diseño y la estetica del mismo, lo estoy poniendo entre mis favoritos, saludos cordiales.-

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

    ReplyDelete
  6. 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
  7. wow now i understood it very well. thank you

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

    ReplyDelete
  9. 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
  10. 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
    Replies
    1. Did you follow all the steps correctly?

      Delete
  11. that was great tnx very much :)))))))))

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

    ReplyDelete
  13. 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
  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

Follow by Email