don't worry it's okay getting data from MySQL database can be extremely
confusing and this video I'm going to show the simple straight on how to
make it stay with me
hey what's up guys, Senaid here from codingpassiveincome.com
the place where I help others to become a web developer much easy and faster
then they will do it on they own so if that is something that interests you consider
subscribing so before we even start we think the
code I want to explain with the basics that you need to understand for this
tutorial so let's say we have some country table inside our MySQL database
and here's what you see inside this table so we have columns ID name and
about and we have some rows here so first thing that we need to do from PHP we
need to write an SQL query that to position ourselves on this table so our
query will look like so let's say you have one variable as well equal and
let's say here we have some connection variable so let's say this variable is
defined before this and it's just a connection to our MySQL database I will
show that in a moment and here we will just say query and inside here I will
just write an SQL so it's very simple let's say you that say
select everything from country and that's it that is that simple SQL query
and I would ask goes so what this query will do it will actually select complete
table and that's it and now what we need to do is we need some how to position
ourselves on this first row get all data after that position second row third
etc so the best way to achieve that is to use while loop because at the
moment we don't know how many rows we have and there is no reason for us to
forget the number of the rows and after that to use any other the best ways to
use while does my centers but it is possible to achieve the same result with
other works so I will say while and let's see here I will create a new
variable data=this SQL and he will say fetch array
and they of course this and let's say well open and close here so what this
part of the code is doing we have wild that will go through each and every row
in this table but this part here will position ourselves at the moment on the
first row we are going to select all the data here okay this and this data will
be transferred to this variable data so after this part of the code executes we
will have create new variable data and let me just show you how it will look
like so we will have data and it is going to be an associative array so I
would have write like we are making it ourselves
and here we will have ID 1 we will have name which is Austria
and they will have about which is okay and they are going to put it so now for
those of you that don't know how to get the enemy's first you need to know that
this is our key inside this array and this is the value for this key so again
this is key this is value etc it now produces food I don't know how to use
associative array all we need to do is to write the name of the variable so I
will say here data and after that here we need to specify the key for
which we want to get the data so let's say I want to get this value I will
first say the key is name and that's it now I can just say echo and post will be
written on our screen so this is that quick explanation of associative array I
have another tutorial that goes really deep on this topic so I will say echo
and now let's say data data and let's say ID. ' '.
and let's say an empty space again which means we are drink
concatenation of our string so it will be ID an empty space for let's say full
stop and ask that an empty space and let's say the country name which is data
name and let's say we want to make new wine or two new lines and then we will
say about
and let's see that's it so now once you add it to this part of the code what
will happen we are selecting the first row we are printing on our screen the ID
name and about from this table after that once we are healed the code will go
here again position ourselves here on the second row so now we are selecting
this one the coded active executing again printing now the number two
Germany about going against checking there is more rules also that
we are position here getting Dix etc and that is the complete explanation and
I guess it's a very basic to understand this so let me now show you how to write
a code so right now I'm here at my PHPstorm and I have created a new project
and inside this project I heard us a basics index.php file with context you
can see here and for those of you that want to download the source code for
this tutorial you will find in the description below the next thing that we
will need is start your XAMPP server and also inside phpmyadmin' create new
table let's say new database what they get in my as well and inside this
database I have a table country which contents you can see here and also I will
include the SQL file with the source code tool so we are just going to use
the data from this table so we have ID name and country code so let's say I'll
write here PHP code and here I will defer define a new variable connection
and I will say $conn = new mysqli and inside here I'm going to write
the connection parameters so first as you can see its host so if you are using
sublime text brackets at them or any other just don't write host because you
don't need it that's right the other things so workout host my
username is root my password is empty and my database name is gettingMySQL
well and that's it so now if you remember we need to define your variable
mysqli and we'll see connection query and inside here we'll write our SQL
so we will say select everything from
country and we will say while data as well fetch array this will happen and
inside here so that output data so we will say Echo $data [ID] .' 'and here let's
make a space .$data[name] space again .$data[countryCode] and let's say let
us make a new line so let's see what will happen
so I will go to the localhost getting later from MySQL using php and as you can
see here we have just a list of all the data that we have inside my SQL so first
one is ID the second one is the name and last one is the country code and
this is the simple straight to get data now let's put all the data inside table
so what I'm going to do here I'll define table and say border equal 1 and I will
know this code up here and let's say you will define one new
row column is ID name country code and inside here I will say table row and
let's say table column okay and I will close this cone shall go down
hill creat new column okay close this column
and I will close the row so this if we didn't make a mistake should create nice
table so let's see so yeah pretty nice and the data look much better so now for
those of you that want to make it a little bit nicer I'm going to show you
how to input bootstrap so I'm going to geetbootstrap.com I will go to the
download bootstrap and down here we have a links for those libraries so I'll just
copy this and include inside head part of our website and let's say here I will
change this at class table table for table borders
so let's see this so how nice this is looking now and we can also add some
containers to make it even nicer so I will say a class container and inside this
container we are going to have one row and inside this row we are going to have
let's say col-MD-8 col-md-offset-2 and the offset two so those are just from the bootstrap
are already predefined with the CSS so you does need to learn how to use it but
for now we are not focusing on learning boost up so that word as is and let's
see what will happen okay so look now it's centered on the screen and the
table and the date that are looking great we even have a whole effect etc so
guys pretty much that's it I hope so that this is an
easy-to-understand tutorial and now you know how to create a nice looking table
for those of you that doesn't understand something or have any other questions
just be sure to comment below and also if you like this video please like it
and save your friends take care
No comments:
Post a Comment