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");
}
CodePudding user response:
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!";
}
CodePudding user response:
You are not comparing the $abc, use == for comparsion, and you are not running any queries, try:
// Database connected successfully
$sql = "SELECT city FROM users ORDER BY id DESC LIMIT 1";
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
$abc= $mysqli -> query($sql)->fetch_all(MYSQLI_ASSOC);
var_dump($abc); // Check exactly what your query returns to correct the code below
if ($abc[0] == "London") {
myFunction();
}
else{
echo("not found");
}