wad11656 Posted January 22, 2019 Share Posted January 22, 2019 (edited) I'm working on a project where the user inputs a URL like these: http://localhost/project/read.php?user=user&secretkey=pass&table=customer&order=age&conditions=age%20BETWEEN%20%270%27%20AND%20%2760%27 http://localhost/project/read.php?user=user&secretkey=pass&table=customer&order=age&limit=0,3&conditions=customer_name%20LIKE%20%27J%%27%20AND%20customer_name%20IN%20(%27Joe%27,%27Bob%27) in the browser window and JSON of the SQL query results is displayed on a .php page. Our instructor gave us the following code "that will help binding the conditions:" $revised_conditions = str_replace("%20"," ",$clean_conditions); $revised_conditions = preg_replace("!'%?[a-zA-Z0-9]+%?'!","?",$revised_conditions); $conditions_array = preg_match_all("!'(%?[a-zA-Z0-9]+%?)'!", $clean_conditions, $condition_matches, PREG_PATTERN_ORDER); ... and ... if ($clean_conditions != False) { $types = ""; foreach ($condition_matches[1] as $c) { if (preg_match("![0-9\.]+!",$c)) { $types .= "d"; } else { $types .= "s"; } } $stmt->bind_param($types, ...$condition_matches[1]); } Can someone tell me what "binding the conditions" means? I believe it means putting each condition from the URL into a variable. How does the above code do that? How would I implement this code to make it functional? Edited January 22, 2019 by wad11656 Quote Link to comment https://forums.phpfreaks.com/topic/308210-dumb-questionin-a-url-sql-query-why-do-you-need-to-bind-the-conditions/ Share on other sites More sharing options...
requinix Posted January 22, 2019 Share Posted January 22, 2019 Your instructor is an idiot. That code has no business being given to anyone learning PHP. Try not to learn too much from it. The principle behind binding parameters is that you write the overall structure of the query without any data inside it, then tell MySQL that certain locations within it (like those marked with ?s) correspond to certain values (the bound parameters). The idea is that you can't accidentally confuse syntax with data. Problem with this code is that it isn't so much "accidentally confusing syntax with data" as it is intentionally confusing syntax with data. Which is terrible, and why you should try to ignore it as much as possible and focus on the basic concepts of prepared statements. 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/308210-dumb-questionin-a-url-sql-query-why-do-you-need-to-bind-the-conditions/#findComment-1563777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.