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...
1
  • then need to show your query and in mehtod two you are converting in string of an object Commented Apr 11, 2014 at 6:52

4 Answers 4

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}'

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, i seem to get this error when using the "{}" : 'Error: Commands out of sync; you can't run this command now
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.
1

Try like this

$sql .= "UPDATE
jobs
SET
CompletedBy = $tech
WHERE JobID = '".$comm[$i]->JobID."'";

3 Comments

Hi, ive tried that it works ok but it has problems with my WHERE clause (check Query edit of main post)
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?
Check this post for error : stackoverflow.com/questions/14554517/…
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

Comments

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);.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.