1

having a bit of an issue here. I'm passing data over an AJAX command to my php script. The data travels across fine.

Print_r

Array 
( [0] => stdClass Object 
    ( [Loc] => Main Door 
      [Module] => O2 
      [Dect] => O2 
      [GasDec] => O2 
      [ScB4] => 
      [ScA4] => 
      [A1] => 
      [A2] => 
      [CalGas] => 
      [CalGasA] => 
      [Factor] => 
      [ZB4] => 
      [ZA4] => 
      [CalB4] => 
      [CalA4] => 
      [CHNoID] => 5 
      [JobID] => 3 )
)

I can use The following method just fine but when i use option two it doesn't like it due to this error message:

Catchable fatal error: Object of class stdClass could not be converted to string

Methods

//Method one
echo "JobID: " . $comm[0]->JobID; // result: 3
//Method two
echo "JobID: '$comm[0]->JobID'"; // get error message

The reason im using method two is so I can pass the information into mysql. If anyone knows something i'm missing or it can't be done or even a easier way. please say.

Thanks.

EDIT

Query

$sql = "INSERT INTO
  calinfo (Sbefore, Safter, A1, A2, CalGas, Factor, Zbefore, Zafter, Cbefore, Cafter, SysInfoID)
  VALUES
  ('$comm[$i]->ScB4', '$comm[$i]->ScA4', '$comm[$i]->A1', '$comm[$i]->A2', '$comm[$i]->CalGasA', '$comm[$i]->Factor', '$comm[$i]->ZB4', '$comm[$i]->ZA4', '$comm[$i]->CalB4', '$comm[$i]->CalA4', '$comm[$i]->CHNoID');";

  $sql .= "UPDATE
  jobs
  SET
  CompletedBy = $tech
  WHERE JobID = '$comm[$i]->JobID';"; //<-- when i try the method of "WHERE JobID = ".$comm[$i]->JobID.";"; it doesnt like it...
Aero204
  • 99
  • 10

4 Answers4

2

You may need to do as

echo "JobID: '{$comm[0]->JobID}'"; 

So in the query u can use it as where some_col = '{$comm[0]->JobID}'

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • Hi, i seem to get this error when using the "{}" : 'Error: Commands out of sync; you can't run this command now – Aero204 Apr 11 '14 at 06:58
  • `WHERE JobID = '{$comm[$i]->JobID}'` is the correct format or WHERE `JobID = "'.$comm[$i]->JobID."'` also you have insert query as well make sure they are all in correct format. – Abhik Chakraborty Apr 11 '14 at 07:01
1

Try like this

$sql .= "UPDATE
jobs
SET
CompletedBy = $tech
WHERE JobID = '".$comm[$i]->JobID."'";
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
  • Hi, ive tried that it works ok but it has problems with my WHERE clause (check Query edit of main post) – Aero204 Apr 11 '14 at 06:59
  • ok works but i get this message: 'Error: Commands out of sync; you can't run this command now. from my sql multi query? any answers? – Aero204 Apr 11 '14 at 07:04
  • Check this post for error : http://stackoverflow.com/questions/14554517/php-commands-out-of-sync-error – Sadikhasan Apr 11 '14 at 07:10
1

As the error message saies, the return cannot be converted to fit into a string directly.

echo sprintf( "JobID: '%s'",$comm[0]->JobID);

or

echo "JobID: '{$comm[0]->JobID}'"; 

or

echo "JobID: '" . $comm[0]->JobID . "'";

should do the trick

Jonas m
  • 2,646
  • 3
  • 22
  • 43
0

Simple double quotes won't interpolate complex combinations like this. You need to use curly braces to achieve this, e.g. echo "JobID: {$comm[0]->JobID}";.

You could however also use printf, e.g. printf("JobID: %s", $comm[0]->JobID);.

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28