Update SQL quary with if condition

How to do this kindly help anybody , its not working

<!DOCTYPE html>

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database_name";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);

$sql = "SELECT id_client, AVG(column_name), id FROM table_name WHERE id = 'field_name'";
$result = $conn->query($sql);

     while($row = $result->fetch_assoc()) {
        $meanduration = $row["AVG(column_name)"];
     if($meanduration > 120){
         mysql_query("UPDATE table_name_2 SET column_name_two='3' WHERE id='field_name'");
     elseif($meanduration < 119)
         mysql_query("UPDATE table_name_2 SET column_name_two='0' WHERE id='field_name'");


That isn’t a column name - you can either reference it by its position in the results or amend the query to assign a column name to that column, for example

AVG(column_name) AS col

But its working that giving me the mean value , The problem is happening to the sql quary in if condition ,
upto if condition its showing out put when i echo $ $meanduration;

So what actually happens that should not, or does not happen that should? Do you get any error messages? What happens if $meanduration actually is 119 or 120, as it won’t run either query then? Do you actually have rows where the id field contains the string “field_name”? I am confused that it’s retrieving data, in fairness, or have you edited that information to not show confidential data?

in database table field value is not updating values, its remain same , not showing any error massege ,

if($meanduration > 120){
mysql_query(“UPDATE table_name SET column_name = ‘3’ WHERE column_name=‘value’”);
elseif($meanduration < 119)
mysql_query(“UPDATE table_name SET column_name =‘0’ WHERE coulmn_name

my problem is value is not updating in data base , $meanduration result is 55.33 , in data base sql is not executing
but if i give any string in the execution place of “if” and “else” condition insted of sql query its giving me right ans , why sql query is not execute and not replacing the value while i reload the page

Why do you use $conn->query() for the first query but mysql_query() for the others?

1 Like

Thanks Brother , Now its working.

Could you tell me whats the difference between this two ?

There are two PHP extensions (libraries with set of functions) to work with MySQL - mysql (old and deprecated one) and mysqli (improved). Both of these extensions have a similar functions (like mysql_query and mysqli_query), but mysqli also allows using object-oriented style ($conn->query()).

In your code you’re using mysqli extension to set a database connection:

$conn = new mysqli($servername, $username, $password, $dbname);

so you have to use mysqli functions in the rest of code too, either mysqli_query or $conn->query

1 Like

I wanna to add sleep(10); so that while loop run in every 10 second then where i have to put this function?

and database value should update in every 10 second automatically .

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.