MySQL Forums
Forum List  »  PHP

Re: How do you dynamically add additional variables to a PDO SQL query?
Posted by: Simon Lewis
Date: November 21, 2019 03:06AM

$params = array();

$sqlStart = "SELECT * FROM users WHERE usertype = 'influencer'";
$sqlEnd = " ORDER BY $col $order LIMIT 0,50";

$sqlFilters = "";

if(isset($_POST["town"])) { $sqlFilters .= " AND town =:town"; $params["town"] = $_POST["town"]; }
$sql = $sqlStart.$sqlFilters.$sqlEnd;
$result = $pdo->prepare($sql); $result->execute($params);

This is something that was sent to me, and kind of makes sense. IT adds the "AND" into the query, and the $params.

This is where I was going with it.

Options: ReplyQuote

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.