json php script fails to return result when query added to try clause

Hi,
I have a piece of Code which 1. sends a sms message and 2. writes a record of this to a log file.

I use Json to return a result "$output" to a calling page in php. It all works fine until I add the   $querylog and $resultlog lines (line 20 and 21 below).
Is seems they are stopping the Json from working.

I've no idea why this would be a problem.

Please see the sample below. Thanks

    try
    {

    $API_KEY = 'xxxxxx';

        // Create a Clockwork object using your API key
       $clockwork = new Clockwork( $API_KEY );

       // Setup and send a message
       $message = array( 'to' => $number, 'message' => $message );
       $result = $clockwork->send( $message );

       // Check if the send was successful
        if($result['success']) 
        {
          $output = 'Message sent - ID: ' . $result['id'];

// If the following query code is added no Json result is returned

        $querylog = "INSERT INTO sms_log (clientcode, mobile, message, type, date, staffid) VALUES ('$id', '$number', '$message', 'Manual', NOW(), '$staffid')";                        
        $resultlog = mysqli_query($dbci,$querylog); //Run the query 

          } else {
        $output = 'Message failed - Error: ' . $result['error_message'];
         }
    }
          catch (ClockworkException $e)
          {
         $output = 'Exception sending SMS: ' . $e->getMessage();
          }


    
echo json_encode($output,JSON_FORCE_OBJECT);

Open in new window

EICTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ludwig DiehlSystems ArchitectCommented:
Here is the problem:
 $querylog = "INSERT INTO sms_log (clientcode, mobile, message, type, date, staffid) VALUES ('$id', '$number', '$message', 'Manual', NOW(), '$staffid')";                        
       

Open in new window


$message is an array, it should be an accepted type (char, int, etc..).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
If you want, you can quickly solve the issue using implode and converting you array to a string.
       
$message = implode(',', $message);

Open in new window


Just add this line before the query:
        if($result['success']) 
        {
          $output = 'Message sent - ID: ' . $result['id'];

// If the following query code is added no Json result is returned
        $message = implode(' ', $message);
        $querylog = "INSERT INTO sms_log (clientcode, mobile, message, type, date, staffid) VALUES ('$id', '$number', '$message', 'Manual', NOW(), '$staffid')";                        
        $resultlog = mysqli_query($dbci,$querylog); //Run the query 

          } else {
        $output = 'Message failed - Error: ' . $result['error_message'];
         }

Open in new window

0
Ludwig DiehlSystems ArchitectCommented:
By the way. I suggest using PDO or the OO version of mysqli

PDO:
 try
        {
            $pdo= new PDO("mysql:host=host;dbname=dbname",'user','pwd');
            $stmt=$pdo->prepare($querylog);
            $resultlog=$stmt->execute(array($id,$number,$message,$staffid));
        }
        catch(PDOException  $e)
        {            
            $output = 'Exception trying to store log: ' . $e->getMessage();
        }

Open in new window

0
EICTAuthor Commented:
This is helpful thank. Turns out that the MySQL query was failing and in order for Json_encode to return a string ( $message ) the preceding command must succeed.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.