0

i have int data in database t_calibrator (calperiod = 12, 24 and 36) of months and i want to calculate with my input date in $callast,

this my created code (but always get 1970-01-01):

$calperiod = mysqli_query($conn, "SELECT calperiod FROM t_calibrator WHERE id_calibrator='$id_calibrator'");
$add = date('Y-m-d', strtotime('+".$calperiod" month', strtotime($callast)));

my reference code get from $add = date('Y-m-d', strtotime('+24 month', strtotime($callast))); and yes that works but only for 24 month.

UPDATE now i try with this code :

$sql = "SELECT * FROM t_calibrator WHERE id_calibrator = '$id_calibrator'";
$result = mysqli_query ($conn,$sql);
$caldata = mysqli_fetch_array ($result);
$add = date('Y-m-d', strtotime('+".$caldata['calperiod']." months', strtotime($callast)));

But showing error like this :

Parse error: syntax error, unexpected 'calperiod' (T_STRING), expecting ',' or ')' in /storage/h7/747/1148747/public_html/pages/calinputhistory.php on line 19

can any one help my this trouble? (sorry for my bad english)

2 Answers2

0

Updated your code like below

$result = mysqli_query($conn, "SELECT calperiod FROM t_calibrator WHERE id_calibrator='$id_calibrator' LIMIT 1");

$row = mysqli_fetch_assoc($result);

$add = date('Y-m-d', strtotime('+'.$row['calperiod'].' months', strtotime($callast)));
Irfan Ali
  • 2,238
  • 1
  • 23
  • 22
  • same like maya, if i use your code, i got this : Recoverable fatal error: Object of class mysqli_result could not be converted to string – Bayu Agustian Mar 23 '17 at 06:13
0

The return from mysqli_query is either a resultset (if query execution was successful) or FALSE if an error occurred. Test the return from mysqli_query, if it's not FALSE, then fetch a row. If we got a row, get the value from the calperiod column.

 $sql = "SELECT calperiod FROM ... ORDER BY ... LIMIT 1";
 if( $sth = mysqli_query($conn,$sql) ) {
     if( $row = mysqli_fetch_assoc($sth) ) {
         $calperiod = $row["calperiod"];  
         echo $calperiod;
         // we have a value ...

     } else {
         echo "no row returned";
     }
 } else {
     echo "error in query execution"; 
     die(mysqli_error($conn);
 }
spencer7593
  • 106,611
  • 15
  • 112
  • 140
  • sorry i'm not understand :( – Bayu Agustian Mar 23 '17 at 06:41
  • The `mysqli_query` function doesn't return a *value* of `calperiod`. It returns a handle to a *resultset*, a reference to a set of zero, one or more rows. If we want to get a *value* from a *column* of a *row* that is in the resultset, we have to *fetch* a row from the *resultset*. – spencer7593 Mar 23 '17 at 16:50