Chris Wright Chris Wright - 5 months ago 4x
SQL Question

How would I pass a parameter into a class function in a php file from Swift/iOS?

Say I have a php file on my web server which contains a class 'Database'. Inside that class is a public function, lets call it 'data_select', which queries MySql database:

public function data_select($query) {
return $rows;

How would I pass in the '$query' parameter to this function from a Swift iOS application?

There are a number of answers online regarding $_POST from swift, but these often concern SQL queries such as 'INSERT INTO ...', whereas my case is a 'SELECT...' query. I also cannot seem to find anything regarding triggering class functions from Swift, although I know it must be possible.

Have I got the wrong idea here? I understand how to pull data from a php file on web server when the sql query and everything is included in the same file, but here the parameters of the query can differ, thus I have the basic SELECT query as part of a database class which can be called with different parameters on my test website, but I cannot work out how to pass in the query parameter from swift and trigger the function.

I guess I could trigger the functions by making a call to a different php file containing an instance of the function with the $query parameter passed in from swift. But would I then need to create multiple php files for each different 'SELECT...' query which sort of defeats the point of having the functions in a class???


PHP is scripting language, mainly used as a server side web language. Swift can communicate with any server side language that uses HTTP. You can use any of the HTTP verbs such as GET, POST, PUT etc to send and receive data from PHP using Swift.

So you'd want to create an endpoint with PHP. You can make an NSMutableURLRequestin swift to send data as a URL parameter.

Take a look at this link to make a GET request in swift.

And take a look at this link to handle a GET request in PHP.

For the multiple files part of your question, you could put many of these endpoints all within one file but that is an implementation and design choice that depends on how you want your application and server to flow.