![]() |
VOOZH | about |
In this article, we will discuss how to execute an SQL query and how to fetch its result?
We can perform a query against the database using the PHP mysqli_query() method.
Syntax: We can use the mysqli_query( ) method in two ways:
Parameters:
Return value: For SELECT, SHOW, DESCRIBE, or EXPLAIN it returns a mysqli_result object. For other successful queries, it returns true. Otherwise, it returns false on failure.
Let's understand how we can execute an SQL query.
Executing an SQL query: We will understand how we can execute an SQL query with an example. We will create a database, table and then insert some values into it.
Example: Create a database and insert some values into it.
Output:
Database has been created successfully
Creating the table:
Output:
Table has been created successfully
Inserting some values into the table "Emp":
Output:
New record created successfully
Note: Since we have used AUTO_INCREMENT, it will automatically insert the record with "id=1" and for each newly inserted record, it will increase the "id" by one.
Fetching results from the database:
Output:
id: 1 - Name: XYZ ABC