Case Insensitive Search with Swift, PHP and MySQL

In these 3 videos I am going to share how to implement case insensitive search with Swift, PHP and MySQL. I am going to use UISearchBar to let user type their search word and then send HTTP POST request to a server side PHP script which will connect to MySQL database and will perform SELECT SQL query. Found records will then be returned in a form of JSON document back to our Swift mobile app and be displayed in a UITableView.

Also, check out my new video course http://goo.gl/gTxpwH if you would like to learn how to implement essential features like User sign up, Sign in, Password reset, Email verification, Profile details update and more with Swift, PHP and MySQL.

Download Xcode project source code

Download PHP scripts source code

  • Aravind

    hi
    i am getting the error could you tell me what to do to solve it

    • This error means that your php script returns something that is not in JSON format. Probably there is an error in php script and the response sent of course is not in JSON format. Can you check your php script to see where could be an issue?

      • Aravind

        This is the php script
        mysql:
        dbhost = $dbhost;
        $this->dbuser = $dbuser;
        $this->dbpass = $dbpassword;
        $this->dbname = $dbname;
        }

        public function openConnection() {
        $this->conn = new mysqli($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname);
        if (mysqli_connect_errno()){
        throw new Exception(“Could not establish connection with database”);
        }
        $this->conn->set_charset(“utf8”);
        }

        public function closeConnection() {
        if ($this->conn != null)
        { $this->conn->close();}
        }

        public function findFriends($searchWord, $userId)
        {
        $returnValue = array();
        $sql = “select * from friends where (first_name like ? or last_name like ?) and user_id=?”;

        $statement = $this->conn->prepare($sql);

        if (!$statement)
        { throw new Exception($statement->error);}

        $searchWord = ‘%’ . $searchWord . “%”;
        $statement->bind_param(“ssi”, $searchWord , $searchWord, $userId);

        $statement->execute();

        $result = $statement->get_result();

        while ($myrow = $result->fetch_assoc())
        {
        $returnValue[] = $myrow;
        }
        return $returnValue;
        }

        }

        ?>

        friends:
        openConnection();

        $friends = $dao->findFriends($searchWord,$userId);

        $dao->closeConnection();

        $returnValue[“friends”]=$friends;

        echo json_encode($returnValue);

        ?>

        could you help with php code by checking

        and thanks for replying

        • I think your php script is not complete but if it is then I see a few potential issues:
          this is how you close connection: $dao->closeConnection(); but this is how you open connection: openConnection(); which I think should be $dao->openConnection();

          also in your code I see : dbhost = $dbhost; which I think should be $this->dbhost = $dbhost;
          ….

  • ray

    Hi!
    I m getting this error also:

    “UISearchBarExampleCaseInsensitiveSearch[25402:1875964] PostData: searchWord=Json&userId=23

    Error Domain=NSCocoaErrorDomain Code=3840 “JSON text did not start with array or object and option to allow fragments not set.” UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}”

    In Conn.php I made the change(localhost, pass…) and upload to my server. I create the database exactly how in the video.
    In the project I just change de url: myserver.com/findFriend.php i don’t touched nothing more.
    Any idea how to fix this would greatly appreciate it..

    Also I used Xcode 7

    • Add these three lines at the top of your php script. This will turn on all error messages. Run your script again but this using the browser window and passing request parameters via the url query string like so: myScript.php?searchWord=something. Will you get a different response? What does it print?

      Also, when you run your php script via the browser window, you will need to read request parameter values using $_REQUEST[“”] rather than $_POST[“”] and when done testing, do not forget to change it back to $_POST[]. $_REQUEST will handle both $_GET and $_POST but it is a good practice to using $_POST for HTTP POST only and $_GET for HTTP GET only when your script is ready to go into production environment.

      ini_set(‘display_errors’, 1);

      ini_set(‘display_startup_errors’, 1);

      error_reporting(E_ALL);

  • badr-eddine ouchhida

    Hi thank you for the good tutorial, can you update to the latest version of swift and Xcode 8 ?
    because I got this error :
    Cannot invoke ‘dataTask’ with an argument list of type ‘(with: NSMutableURLRequest, completionHandler: (Data?, URLResponse?, NSError?) -> Void)’

  • David Ventura

    Hi could you provide an output of your son file to make sure my results are correct.

    Thanks