-4

I have the following PHP code:

<?php
header("Access-Control-Allow-Origin: *");

include_once('../includes/mysql_connect.php');
include_once('../db/tables/search.php');

// SAVE TO DB
$search = new search($mysql);
//$search->drop();
//$search->create();
$search->save(array("shop" => "test store", "searchterm" => $_POST['search']));


// LOG TO FILE
$file = fopen('search-data.txt', 'a+');
$result = fwrite($file, "\nsearch query data: " . print_r($_POST['search'], true) );    

// SEND RESPONSE
$response = array('response' => 'received', 'data' => $_POST['search'] );
echo json_encode($response);

When I look on the browser console I see this:

<br>INSERT QUERY SUCCESSFUL: insert_id=69{"response":"received","data":"product"}

The "INSERT QUERY SUCCESSFUL" is coming from my DB class which does the INSERT and echos out various messages. How can I show only the JSON response in the last two lines of code and not these echoed out messages? I want to keep these messages in my DB class for the moment. I wouldn't have expected anything else other than the data in the response array to be displayed in the console.

5
  • 1
    Apart from that fact that it's weird to leave logs via echo, you're probably looking for ob_end_clean(), which will just erase everything echo produced up until the command execution. That said, there're specific instruments for logging that should be used for this purpose
    – nicael
    Commented Jan 27, 2023 at 20:26
  • @nicael yes I'm aware, its just for the moment, not permanently. Its just for some quick testing, nothing else. I'll check out that function, might do for now. Thank you :) Commented Jan 27, 2023 at 20:31
  • stackoverflow.com/questions/6079492/how-to-print-a-debug-log is probably the real answer here. Don't work around this issue with ob_end_clean.
    – user229044
    Commented Jan 27, 2023 at 20:34
  • If you don't expect them to be shown in the browser, where do you expect them to go?
    – Barmar
    Commented Jan 27, 2023 at 20:34
  • @Barmar good point! I wasn't expecting them to be included along with the response, but clearly and obviously that would be the case. Commented Jan 27, 2023 at 20:41

1 Answer 1

2

Don't use echo as a logging mechanism. It's specifically for sending content to the browser. If you want to log things, you need to log them to a file or via error_log, not via echo.

I wouldn't have expected anything else other than the data in the response array to be displayed in the console.

This is completely incorrect, and there is absolutely no reason to expect this. $response is just a random variable, no different from any other variable. PHP doesn't know that you wanted one invocation of echo to behave differently than your other ones, just because of the name of the variable you passed to it. Everything you echo goes to the browser, by design. You could try to work around this with output buffers, but don't. Don't use echo for logging, this is fundamentally at odds with how PHP works.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.