0

I want to run a function if the returned data from database matches a specific value.

For example, if SELECT city FROM users ORDER BY id DESC LIMIT 1 returns "London" then run the PHP function 'myFunction()'.

What I tried is :

// Database connected successfully
$abc = "SELECT city FROM users ORDER BY id DESC LIMIT 1";
if ($abc = "London") {
myFunction();
}
else{
echo("not found");
}
m24197
  • 1,038
  • 1
  • 4
  • 12

1 Answers1

2

You should follow these steps.

//build database connection
$conn = mysqli_connect("localhost","my_user","my_password","my_db");

//query
$query = "SELECT city FROM users ORDER BY id DESC LIMIT 1";

//execute query
$resultSet = mysqli_query($conn, $query);
$record = mysqli_fetch_assoc($resultSet); // as we have limit 1 in query so only one record will be returned in result set

if($record['city'] == 'London'){
    //call function
    myFunction();
}else{
    echo "Not found!";
}
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30