Question

I have following query which works fine for me on my localhost and sets current time in database.

$query="INSERT INTO mytable (user_id, user_datetime) VALUES ".
       "(".$_SESSION["user_id"].", NOW());";

But when I hosted my website on a US server, it is putting US current time in database, which is undesirable. I want to put Indian current time.

For that I am modifying above query as

$query="SET time_zone = '+05:30';INSERT INTO mytable (user_id, user_datetime) VALUES ".
       "(".$_SESSION["user_id"].", NOW());";

But this is not working. Although if I run SET time_zone = '+05:30'; on mysql directly, it works? Where I should look to resolve it?

Was it helpful?

Solution

you have to change the quotes like that:

 $query="INSERT INTO mytable (user_id, user_datetime) 
         VALUES ( '".$_SESSION['user_id']."', NOW())";

SET is for update Not insert.

if you want insert also time_zone

 $query="INSERT INTO mytable (user_id, user_datetime ,time_zone) 
         VALUES ( '".$_SESSION['user_id']."', NOW() ,'+05:30' )";

if you already have values you may look for update.

 $query="UPDATE mytable SET time_zone = '+05:30' 
         WHERE user_id ='".$_SESSION['user_id']."'  ";

Edit: you cant run two queries in one . you must run both queries

$query1="SET time_zone = '+05:30' " ;
$query="INSERT INTO mytable (user_id, user_datetime) 
         VALUES ( '".$_SESSION['user_id']."', NOW())";

OTHER TIPS

from Php manual

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

so move to mysqli or execute each query separately

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top