-1

Hi there and thank you for the help. I will try to be breef.
I have a SQL table with one col named "duration" -> type Time
I need to get this "duration" and add to the actual DateTime -> date()
Till now I got something like these:

$id_mission = $_POST["id_mission"];
$sql="SELECT duration FROM missions WHERE id_mission='".$id_mission."'";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

How do i pick this: $row['duration'] and convert to DateTime Object?

$date = new DateTime();
$get_datetime = $date->format('Y-m-d H:i:s');
$get_datetime->add(new DateInterval($row['duration']));

I got these sql error:

Fatal error: Call to a member function add() on string in C:\wamp64\www\players\actions\insert_mission.php on line 18

Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
  • 1
    Your code is vulnerable to [**SQL injection**](https://en.wikipedia.org/wiki/SQL_injection) attacks. You should use [**mysqli**](https://secure.php.net/manual/en/mysqli.prepare.php) or [**PDO**](https://secure.php.net/manual/en/pdo.prepared-statements.php) prepared statements with bound parameters as described in [**this post**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – Alex Howansky May 31 '17 at 21:04
  • Alex Howansky Thank you for the advice. –  May 31 '17 at 21:08

1 Answers1

1

This line converts your DateTime object into a string which you are then trying to call the add() method with. Strings don't have this method.

$get_datetime = $date->format('Y-m-d H:i:s');

To add the date from your row don't use the format method.

$date = new DateTime();
$date->add(new DateInterval($row['duration']));

The error message that you are seeing has nothing to do with SQL, it is telling you that your are trying to treat a string like an object. Which doesn't work in PHP.

Schleis
  • 41,516
  • 7
  • 68
  • 87
  • Already tried this, the problem is that as I say in the Post: duration is type Time format 00:01:00 and when I do that: I got a error, because $row duration is not the same type as DateTime. –  Jun 01 '17 at 08:08