Retrieving Data from Mysql/Mariadb

In the previous lesson, we learnt how to send data to the database. Now it’s time to retrieve data from our database to our PHP pages.

This is really one of the most important lessons in this book. Once you have read and understood this lesson, you will realize why database-driven web applications are so robust.

To retrieve data from a database, you use select queries like –

SELECT * FROM employees

Data display from database to the web page will process in two steps –

– The very first it will count the number of rows. You should know the number of the records in your table.

– Secondly we have to set the loop accordingly. Each time it will be incremented by one. It will fetch each row of the result and print the data stored there.

There are some popular methods to retrieve records from the database in PHP-

mysql_fetch_row(): It will fetch result row as an numeric way. it returns a row where the values will come in the same order as they are defined in the SQL query.

mysql_fetch_assoc():It will fetch a result row as an associative array. This function will return a row as an associative array where the column names will be the keys storing corresponding value. Means we can fetch our record through the table column name.

mysql_fetch_array(): It will fetch a result row as an associative array or a numeric array. So this is the combination of both the method of mysql_fetch_row and mysql_fetch_assoc.

mysql_fetch_object(): It will fetch a result row as an object

Example 1: To retrieve data from Mysql Table- Lets retireve records of recently added table as below-

Database Name: mydb

Table Name: employees

Column List: name, email, password, phone

<?php

// Prepare a database connectivity first

$con = mysqli_connect(“localhost”, “root”, ” “, “mydb”);

// Prepare SQL Statements to fetch record

$query = mysqli_query(“SELECT  * from employees”;

// Use any fetch method to retrieve records

while ($row = mysqli_fetch_assoc($query))

{

/* Now you can display columns using $row variable as all records to be proceed by the $row variable */

echo $row[‘name’]. “<br>”;

echo $row[‘email’]. “<br>”;

}

/* close connection */

mysqli_close($con);

?>

 

87 thoughts on “Retrieving Data from Mysql/Mariadb

  1. I needed to thank you for this very good read!!

    I definitely loved every little bit of it. I’ve got
    you book-marked to look at new things you post…

  2. After I initially commented I appear to have clicked the -Notify me when new comments
    are added- checkbox and now whenever a comment is added I get
    4 emails with the same comment. Perhaps there is an easy method you can remove me from
    that service? Thank you.

  3. After I originally left a comment I seem to have clicked the -Notify me when new comments
    are added- checkbox and from now on each time a
    comment is added I get four emails with the same comment. There has to be an easy
    method you can remove me from that service?
    Appreciate it.

  4. I really love your blog.. Pleasant colors & theme. Did you make this
    amazing site yourself? Please reply back as I’m trying to create my own personal website and would like to find
    out where you got this from or what the theme is called.
    Appreciate it.

  5. You are so interesting! I do not believe I’ve read through anything like that before.
    So great to discover another person with unique thoughts on this issue.
    Really.. thanks for starting this up. This site is one thing that is needed on the web, someone with some originality.

  6. This is the perfect webpage for anybody who really wants to understand this topic.
    You know a whole lot its almost tough to argue with you (not that I actually would want
    to…HaHa). You definitely put a fresh spin on a topic that has been written about for many years.
    Wonderful stuff, just excellent.

  7. Having read this I thought it was rather informative.
    I appreciate you spending some time and effort to put this content
    together. I once again find myself personally spending a significant amount of time both reading and leaving
    comments. But so what, it was still worthwhile!

  8. Hi there, I think your blog could be having web browser compatibility issues.
    When I look at your web site in Safari, it looks fine but when opening in IE, it has some overlapping issues.
    I just wanted to give you a quick heads up! Besides that,
    wonderful blog!

  9. Howdy! This article couldn’t be written much better!
    Looking through this article reminds me of my previous roommate!
    He always kept talking about this. I am going to forward this article to him.

    Pretty sure he’ll have a great read. Thanks for sharing!

  10. Your style is so unique compared to other folks I’ve read
    stuff from. I appreciate you for posting when you’ve got the
    opportunity, Guess I will just book mark this site.

  11. Nice post. I learn something new and challenging on blogs I stumbleupon every day.

    It will always be interesting to read through content from other writers and practice a little something from other web sites.

  12. Hi, I do think this is a great site. I stumbledupon it 😉 I’m
    going to return yet again since i have saved as a favorite it.

    Money and freedom is the best way to change, may you be rich and continue to help other people.

  13. I’m impressed, I have to admit. Rarely do I encounter a blog that’s both equally educative and amusing, and without a doubt, you’ve hit the nail on the head.
    The problem is something not enough people are speaking intelligently about.
    Now i’m very happy I came across this during my hunt for something
    relating to this.

  14. I’m amazed, I must say. Seldom do I come across a blog that’s equally
    educative and engaging, and without a doubt, you have hit the
    nail on the head. The problem is something not enough men and women are speaking intelligently about.

    I’m very happy that I stumbled across this during my search for something regarding
    this.

  15. You are so cool! I don’t believe I’ve truly read through anything
    like that before. So wonderful to discover another person with
    unique thoughts on this subject matter. Really..
    thank you for starting this up. This site is something that is required on the internet,
    someone with some originality.

  16. When I initially commented I appear to have clicked
    on the -Notify me when new comments are added- checkbox and now
    every time a comment is added I get four emails with the exact same comment.
    Is there a means you are able to remove me from that service?
    Thanks a lot.

  17. Greetings, I think your website may be having internet browser compatibility
    issues. Whenever I take a look at your blog in Safari, it looks fine however, if
    opening in I.E., it’s got some overlapping issues.
    I merely wanted to give you a quick heads
    up! Besides that, great website!

  18. Having read this I believed it was really informative.
    I appreciate you finding the time and effort to put this information together.

    I once again find myself personally spending a lot of time both reading and commenting.
    But so what, it was still worthwhile!

  19. Oh my goodness! Incredible article dude! Thank you,
    However I am having troubles with your RSS. I don’t understand the reason why I cannot
    subscribe to it. Is there anybody getting identical RSS problems?
    Anyone who knows the answer will you kindly respond? Thanx!

  20. Nice post. I learn something new and challenging on blogs I stumbleupon every day.

    It will always be useful to read content from other authors and practice a little something from their websites.

  21. I’m impressed, I have to admit. Seldom do I encounter a blog
    that’s equally educative and entertaining, and
    let me tell you, you’ve hit the nail on the head. The issue is
    something that too few people are speaking intelligently about.
    I’m very happy that I came across this during my search for something relating to this.

  22. A motivating discussion is definitely worth comment. I think that you need to write more on this subject matter, it might
    not be a taboo matter but usually people do not speak about these issues.
    To the next! All the best.

  23. You’ve made some really good points there. I checked on the
    internet for more info about the issue and found most people will go along with your views on this web site.

  24. Hi there! I could have sworn I’ve been to your blog before but after browsing through a few of the posts
    I realized it’s new to me. Anyhow, I’m definitely happy I stumbled upon it and
    I’ll be bookmarking it and checking back often.

  25. You have made some decent points there. I checked on the web for additional information about
    the issue and found most people will go along with your views on this
    website.

  26. A fascinating discussion is worth comment. I do think that you should write more about this subject, it might not be a taboo matter but usually people don’t speak
    about these issues. To the next! Many thanks.

  27. When I initially commented I seem to have clicked on the -Notify me when new comments are added- checkbox
    and now every time a comment is added I get four emails with the same comment.

    Is there an easy method you are able to remove me from that
    service? Thanks a lot.

  28. I blog frequently and I truly thank you for your information. This article has truly peaked my interest.
    I will bookmark your site and keep checking for new details about once per week.

    I subscribed to your RSS feed too.

  29. That is a very good tip particularly to those new to the
    blogosphere. Simple but very accurate information… Many thanks for sharing this one.

    A must read post!

  30. You’re so interesting! I don’t believe I’ve truly read something like
    this before. So wonderful to discover somebody with a few original thoughts on this topic.
    Really.. thanks for starting this up. This site is one thing that
    is needed on the web, someone with a bit of originality.

  31. I’d like to thank you for the efforts you’ve put in penning this
    blog. I’m hoping to check out the same high-grade content from you later on as well.
    In truth, your creative writing abilities has motivated me to get my own, personal
    website now 😉

  32. Hello there, I do believe your site could be having internet browser compatibility problems.
    When I look at your web site in Safari, it looks fine however when opening in IE,
    it has some overlapping issues. I simply wanted to provide you with a quick
    heads up! Besides that, wonderful website.

  33. I’m amazed, I have to admit. Rarely do I come across a
    blog that’s equally educative and interesting, and let me tell you, you’ve
    hit the nail on the head. The problem is something not enough folks are speaking intelligently about.
    I am very happy that I came across this in my search for something regarding this.

  34. I truly love your website.. Pleasant colors & theme.
    Did you build this site yourself? Please reply back as I’m hoping to create my own blog and would like to know where you got this from
    or exactly what the theme is called. Cheers.

  35. An intriguing discussion is worth comment. I do think that you ought to write more on this subject, it may not be a taboo matter but generally people don’t
    talk about these subjects. To the next! Cheers.

  36. When I initially commented I seem to have clicked on the -Notify me when new comments
    are added- checkbox and from now on whenever a comment is added I
    recieve 4 emails with the same comment. Is there a
    means you are able to remove me from that service?
    Appreciate it.

  37. Aw, this was an incredibly good post. Spending some time and actual effort
    to generate a good article… but what can I say… I hesitate a whole lot and never seem to get anything
    done.

  38. Aw, this was a very nice post. Taking a few minutes and actual effort
    to create a great article… but what can I say… I hesitate a whole lot and don’t seem to get
    anything done.

  39. Aw, this was an exceptionally nice post. Finding the time and actual effort to make a superb article… but
    what can I say… I procrastinate a whole lot and never seem to get anything done.

  40. Howdy, I do believe your site might be having browser compatibility issues.
    When I look at your website in Safari, it looks fine however,
    when opening in IE, it’s got some overlapping issues.
    I merely wanted to give you a quick heads up! Other than that, fantastic blog!

  41. I blog frequently and I genuinely thank you for your information. This
    great article has really peaked my interest. I’m going to bookmark
    your site and keep checking for new information about once a week.
    I subscribed to your Feed as well.

  42. This is the right webpage for anyone who wishes
    to find out about this topic. You know so much its almost tough
    to argue with you (not that I really will need to…HaHa).
    You definitely put a fresh spin on a subject that has been discussed for
    ages. Great stuff, just wonderful.

  43. I blog frequently and I seriously thank you for your
    content. This great article has really peaked my interest.
    I’m going to take a note of your blog and keep checking for new information about once per week.

    I subscribed to your RSS feed as well.

  44. Greetings, I do believe your website could possibly be having browser compatibility issues.
    When I take a look at your blog in Safari, it looks fine however when opening in I.E., it’s
    got some overlapping issues. I simply wanted to give you a quick heads up!
    Aside from that, excellent site.

  45. When I originally commented I seem to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I
    get four emails with the exact same comment. Perhaps there is a means you can remove me from that service?

    Many thanks.

Leave a Reply

Your email address will not be published.

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/04/22/retrieving-data-mysqlmariadb
YouTube43
YouTube