Home
/
PHP & MySQL Tutorials
/
How to Query Your MySQL Database Tutorial

How to Query Your MySQL Database Tutorial

If you want to use PHP to query your MySQL database you can do that by either entering the MySQL query command in the PHP script or define the command as a variable and use the variable when needed.

This tutorial explains how to perform the query by using a variable. In that case, the command will look similar to:

mysqli_query($query);

The command can be repeated again in the source code. All you need to do is to change the $query variable.

For example, here is the complete code that could be used to create a MySQL table in PHP:

<?php
$username = "your_username";
$password = "your_password";
$database = "your_database";
$mysqli = new mysqli("localhost", $username, $password, $database);
$query="CREATE TABLE tablename(id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,field1-name varchar(20) NOT NULL,field2-name varchar(20)NOT NULL,field3-name varchar(20) NOT NULL,field4-name varchar(30) NOT NULL, field5-name varchar(30)NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
$mysqli->query("$query");
$mysqli->close();
?>

Replace your your_username, your_password and your_database with their actual values in the first three lines of the script.

You can replace the value of the $query variable with any MySQL query you want and you can use the above format to execute it.

Share This Article