There a three types of Database connections, or API's, in PHP; to keep this to the point, I am going to cover PDO
.
Your question: Can I use complex SQL in PHP, meaning put anything into the query?
The Answer: Yes, you can - in fact, its just like running it inside the Database.
PHP is a weakly typed language meaning you do not need to use Ragex to do complex sequences.
An example here is joining rows for a deletion:
// Connecting to the database via PDO
$dsn = 'mysql: host=localhost; dbname=example';
$user = "user";
$password = "pass";
try {
$db = new PDO($dsn, $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
// The query
$stmt = $db->prepare("DELETE t2.* FROM table t1 JOIN table1 t2 ON t1.column= t2.column");
if($stmt->execute()): echo 'Successfully deleted!'; endif; // Conditional statements
// note this returns a bool datatype so we can check if it was successful or not
To learn more about PDO you can use this manual.
Note: You may want to read up on SQL Injections if you're taking this approach - PDO
is secure but injections come from user-code sides. (See integration of Classes and Scopes for security when using Databases).
Mysqli example (as requested):
$db = mysqli_connect("localhost","user","pass","name");
if($db->query("DELETE t2.* FROM table t1 JOIN table1 t2 ON t1.column= t2.column")):
echo 'Deleted Successfully!';
endif;