I am very green in PHP. I need to create a small service on server side which will grab data, transform it and populate it into MySQL database.
Everything is fine so far. I managed to create PHP script which updates existing rows in my database.
I have difficulties with checking If I need to update or add rows. Could you help me?
$servername = "localhost";
$username = "almazini";
$password = "3334444";
$dbname = "almazini";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
then I have a loop through my array ... and executing SQL query:
$sql = "UPDATE MyTargetTable SET SRate = $sr, BRate = $br WHERE Name='$name' AND SName = '$surname' ";
$stmt = $conn->prepare($sql);
$stmt->execute();
How can I check if there is a row in MyTargetTable which I should update? If there is no such row - I would like to fire another query to insert new row.