How to execute a sql query in Mysql with PHP

PHP comes with all the required tools for connecting and interacting with a Mysql database in the backend. Although you may not have previous experience with PHP and Mysql, this blog post is self explanatory and truly easy to follow.

Define the PHP script

Before writing any code for our simple PHP script, it is required that we define it like shown below. With your favorite text editor open a new file and save it with a .php extension.

execute_sql_query.php

The initial script should look like the one which is shown below.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

<?php

?>

Define the connection settings

Once you have managed to create a fresh database in the Mysql database management system, you need to define the connection settings like shown below.

<?php

// define the connection settings
$hostname = "";
$username = "";
$password = "";
$database = "";

?>

Create a new connection with the database in the backend dbms

In order to execute sql queries in the database located in the backend dbms it is required that we connect to it through the following utility offered by the PHP computer programming language.

<?php

// define the connection settings
$hostname = "";
$username = "";
$password = "";
$database = "";

// create a new connection with the database
$mysqli = new_mysqli($hostname, $username, $password, $database);

?>

Define the sql query for execution

Before executing a specific sql query in the database, we need to define it with a PHP variable like shown below. It is recommended that each sql query ends with a semi colon.

<?php

// define the connection settings
$hostname = "";
$username = "";
$password = "";
$database = "";

// create a new connection with the database
$mysqli = new_mysqli($hostname, $username, $password, $database);

// define the sql query for execution
$sql = "CREATE TABLE test;";
?>

Execute the sql query

Now that we have managed to define the connection settings, have created a fresh connection with the database management system, it is time to execute our custom sql query. PHP offers the query utility to send and execute a sql query in the backend dbms.

<?php

// define the connection settings
$hostname = "";
$username = "";
$password = "";
$database = "";

// create a new connection with the database
$mysqli = new_mysqli($hostname, $username, $password, $database);

// define the sql query for execution
$sql = "CREATE TABLE test(title varchar(255));";

// execute the sql query
$mysqli->query($sql);
?>

Final thoughts

The execution of a sql query in the backend database management system is an easy thing to do, as PHP comes with all the required tools for the job. Through this blog post you learned how to write a simple PHP script which connects with the database management system in the backend and creates a table with a single field through the predefined sql query.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *