Once you open your MySQL database in your PHP code, it is ready to be queried for data. When retrieving data from a database, it is good practice to put the results of the query in a variable. This allows the for the data to be referenced at a later point in code, after the database has been closed.
Assuming that you have already opened your database, retrieving data from it is only a statement away.
The retrieval statement takes twos parameters:
1. the connection statement that you used to open the database
2. the query statement itself
Since MySQL databases must be queried using SQL, parameter number 2 needs to be an SQL statement,
$mycon=mysqli_connect("example.com","peter","abc123","my_db");
$myresult = mysqli_query($mycon,"SELECT * FROM cities");
The above statement takes all of the data in the "cities" table of "my_db",
and stores it into a variable called myresult.
As mentioned earlier, long after the database connection has been closed, the information in the "cities" table can still be used through referencing the myresult variable.
Deleting information dynamically, through PHP code, works in a similar fashion. The select statement is replaced with a delete statement.
mysqli_query($con,"DELETE * FROM cities");
The above statement will remove all of the data in the "cities" table of "my_db".
Since data is being deleted and not retrieved, there is no need to store the results of the statement. in a local variable.
No comments:
Post a Comment