The first thing we're going to do, once we've connected to the database, is retrieving some data from it.
I hope you remember that we have an email address and a password stored in our database.
So we're going to get that back from the database and display it using PHP.
The first thing I'm going to do is change this code slightly so that it doesn't display anything if the database connection was successful.
But if the database connection wasn't successful, we want to stop the script immediately. We can do that using the die command. If the database connection is not successful, it is wise to not to do any further processing.
Instead of ‘echo out ‘Something went wrong’, use die(“something went wrong”)
Now if database connection fails, the script will stop right away.
The second bit of preparation that we need is to assign a variable to that to our database connection so that we can refer to it later on.
And normally we call that variable $link .
But you can call it whatever you like but I think its pretty standard.
Now it is time to get the data from the database. The first step of doing that is - querying our database.
This is where MySQL comes in.
Anything in between these two quotes will not be in PHP. It will be in MySQL.
So we're learning a whole new language here.
But don't worry it's pretty straightforward.
So if we want to select everything from the user's table within our database then we would just say
“SELECT * FROM”
So this means select everything from and then the table name which is users and that's it.
“SELECT * FROM users”
That's our way of saying selects everything from our users table.
Notice the capitalization, it's not necessary.
This code will work fine without it but it is conventional.
Capitalizing the MySQL syntax makes it easier to distinguish between PHP and MySQL and make it easier to read.
Let’s assign this MySQL code to a variable named ‘$query’.
Again you can call it whatever you like but ‘$query’ is pretty standard.
Now, we just need to run the query using our link connection to this database and we do that in a few steps.
So step one is to actually do the query and we do this using ‘mysqli_query()’.
And then we give the database connection that we want to do the query over.
So in this case link and then we gave the query itself.
Now this function will query the database. However, it will not return anything.
So the way we normally use this would be to put an if statement around it to check to see whether it's been successful.
So if that works it will return true.
To retrieve the data from the database, we will first we assign a variable to the result of the mysqli_connect() function.
Normally we call it result.
So now we can use the variable result to refer to this query and if it was successful then we can get the results from it.
And we do that using myslqi_fetch_array() function.
Then we say that we want to fetch the array associated with this query of this result and normally that will be a row of the database.
So we normally call it a $row.
Let’s print it out.
And there we go.
So our first real proof that we have connected to that database we've got our data back in the form of an associative array with the ID, email and password.
Notice that each variable that can be accessed in two different ways.
It might be slightly clearer if I look at the source.
So you can use 0 1 and 2 to refer to the ID email and password.
Or you can use probably more usefully variable names so Id email and password.
Instead of retrieving the entire array, let’s get a single value.
Simply echo out the $row[“key”] by mentioning the key of the associative array. For example – if we want the email address, then the key will be “Email”.
And we can retrieve the password using $row[“Password”];
That’s all about retrieving data from database.
Obviously being able to select everything from the database is not what you want to do most of the time.
We're going to see how to add and update data in the database and then we'll look at more advanced queries to select the data that we want and also to loop through if we get multiple results in the next lesson.
To perform the query, we need to connect to the database.
Which we can do using this $link.
And then we need to define the query that means what we want to do.
Which has been stored in this $query variable.
Now, using, mysqli_query() function, we can perform the query.
This function requires the link to connect to the database, which is right here.
Then it requires the query itself, which is here.
Now if we run this script, nothing happens. Because this function simply performs the query. It does not give us any data.
However, if the query is not successful, then it returns empty, and generate a warning.
Sur cette page du site, vous pouvez voir la vidéo en ligne MySQL: Retrieving Data from Database durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Nuruzzaman Faruqui 04 août 2020, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 190 fois et il a aimé 6 téléspectateurs. Bon visionnage!