SELECT from mysql DB in app with WHERE condition

Hey Everyone!

First of all: I’m totally newbie, so please do not laugh heavily! :smiley:

I just workin’ on my own self-taught learning project and have a little trouble.

I use a MYSQL DB wich has 2 table:
users and Locations (yes the second one is from the MapKit tutorial, thanks in this way for the tutorial :slight_smile: ).

What is working fine?

  • login,
  • register,
  • location insert to the Locations table
  • location update,
  • location delete

BUT:
I can’t solve that i wanted from this SQL:
SELECT * FROM Locations WHERE user_id=$user_id

I would like send user_id from the app to the service.php, and get back the datas in my tableView.
I don’t know how to hand-over the user_id from the app to the PHP and get the result in one function.

This is my code yet, and yes, there are not the hand-over method, coze’ i cant handle it…:
Xcode:
func downloadItems() {

    let defaultValues = UserDefaults.standard  
    let user_id = defaultValues.string(forKey: "userid") //this is the active loggedin user_id
    
    let urlPath = "http://myserverlocation.com/service.php"
    let url: URL = URL(string: urlPath)!
    let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)
    
    let task = defaultSession.dataTask(with: url) { (data, response, error) in
        
        if error != nil {
            print("Failed to download data")
        }else {
            print("Data downloaded")
            self.parseJSON(data!)
        }
        
    }
    
    task.resume()
}

PHP:
<?php

// Create connection
$con=mysqli_connect("localhost","user","pass","db");
 
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

      $user_id = $_POST['user_id'];

$sql = "SELECT * FROM Locations WHERE user_id='$user_id'";
 
// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();
 
    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }
 
    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}
 
// Close connections
mysqli_close($con);
?>

Thanks in advance for your help!

Joseph