0

I have a db of MySQL queries that I need to output in PHP. The queries include < and > in them and these are getting converted to html tags so the whole query is not outputting. How do I output it without the <> changing?

SELECT
  (CASE WHEN measure_start IS NULL AND fy_month = 1 AND fy_day = 1
    THEN YEAR(measure_date)
   WHEN measure_start IS NULL AND MONTH(measure_date) > fy_month
     THEN CONCAT(YEAR(measure_date), '' - '', YEAR(measure_date) + 1)
   WHEN measure_start IS NULL AND MONTH(measure_date) > fy_month
     THEN CONCAT(YEAR(measure_date) - 1, '' - '', YEAR(measure_date))
   WHEN measure_start IS NULL AND MONTH(measure_date) = fy_month AND DAY(measure_date) >= fy_day
     THEN CONCAT(YEAR(measure_date), '' - '', YEAR(measure_date) + 1)
   WHEN measure_start IS NULL AND MONTH(measure_date) = fy_month AND DAY(measure_date) < fy_day
     THEN CONCAT(YEAR(measure_date) - 1, '' - '', YEAR(measure_date))
   WHEN measure_date IS NULL AND fy_month = 1 AND fy_day = 1
     THEN YEAR(measure_start)
   WHEN measure_date IS NULL AND MONTH(measure_start) > fy_month
     THEN CONCAT(YEAR(measure_start), '' - '', YEAR(measure_start) + 1)
   WHEN measure_date IS NULL AND MONTH(measure_start) < fy_month
     THEN CONCAT(YEAR(measure_start) - 1, '' - '', YEAR(measure_start))
   WHEN measure_date IS NULL AND MONTH(measure_start) = fy_month AND DAY(measure_start)d>=fy_day
     THEN CONCAT(YEAR(measure_start), '' - '', YEAR(measure_start) + 1)
   WHEN measure_date IS NULL AND MONTH(measure_start) = fy_month AND DAY(measure_start) < fy_day
     THEN CONCAT(YEAR(measure_start) - 1, '' - '', YEAR(measure_start)) END) AS ''Year '',
  amount                                                                     AS amount,
  cost                                                                       AS cost
FROM data_operations
  LEFT JOIN data_measure ON data_operations.measure = data_measure.id
  INNER JOIN profile_locationmeta ON data_operations.loc_id = profile_locationmeta.loc_id
WHERE data_operations.id IN (SELECT MAX(id)
                             FROM data_operations
                             GROUP BY parent_id)
7
  • 2
    php.net/manual/en/function.htmlentities.php Commented May 15, 2019 at 18:45
  • 1
    Do you want the query to be displayed inside a website (html)? If thats the case, you should have a look at the php-functions htmlspecialchars and/or htmlentities Commented May 15, 2019 at 18:46
  • I want to use the stored query to perform another query to generate a table. $chart_rows = $wpdb->get_results( "$query"); htmlspecialchars and htmlentities makes the query not work. $query is the one stored in the DB I'm having issues with Commented May 15, 2019 at 18:58
  • 2
    It's worth point out that storing queries in the database is usually a super bad idea as it makes it easier to compromise and inject arbitrary SQL code. For "canned queries" you may want to use stored procedures or views. Commented May 15, 2019 at 19:13
  • 1
    Thanks @tadman creating a view has solved my issue Commented May 15, 2019 at 19:24

1 Answer 1

1

Use htmlspecialchars to echo it on your website.
Assuming that your query is inside the $query variable, that's how you print it

echo htmlspecialchars($query, ENT_QUOTES);

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

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.