How to include a PHP variable inside a MySQL statement
To include a PHP variable inside a MySQL statement, you need to use prepared statements with bound parameters. Here is an example:
<?php
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
In this example, $conn
is a MySQLi connection object, $stmt
is a MySQLi statement object, and $username
is the PHP variable that you want to include in the MySQL statement.
The ?
placeholder represents the bound parameter. The "s"
argument specifies that the bound parameter is a string. If you want to bind an integer value, you will use "i"
instead.
Note that you should always use prepared statements with bound parameters to protect against SQL injection attacks.