php-mysqlHow to use a variable in a MySQL query using PHP?
Using a variable in a MySQL query using PHP is a common task. The following example code block shows how to use a variable in a MySQL query using PHP:
$variable = "example";
$query = "SELECT * FROM table WHERE column = '$variable'";
$result = mysqli_query($connection, $query);
The output of the example code is a MySQL query that looks like this:
SELECT * FROM table WHERE column = 'example'
Code explanation
$variable = "example";- This line of code declares a variable called$variableand assigns it the value of "example".$query = "SELECT * FROM table WHERE column = '$variable'";- This line of code declares a variable called$queryand assigns it a MySQL query that uses the$variablevariable.$result = mysqli_query($connection, $query);- This line of code executes the MySQL query and stores the result in the$resultvariable.
Helpful links
More of Php Mysql
- How to check the result of an insert in PHP and MySQL?
- How to list tables in PHP MySQL?
- How to get the last insert ID in PHP MySQL?
- How to output XML from MySQL using PHP?
- How to set a timeout for MySQL query in PHP?
- How to export data from MySQL to Excel using PHP?
- How to check if a record exists in PHP and MySQL?
- How to create an SSL connection to MySQL using PHP?
- How to get the version of MySQL using PHP?
- How to use utf8mb4_unicode_ci in MySQL with PHP?
See more codes...