PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, May 19, 2022

[FIXED] How can I retrieve a MySql statement as a string after binding?

 May 19, 2022     mysql, parameterbinding, php, sql     No comments   

Issue

I am trying to debug a SQL statement I have not written myself:

    $stmt = $dbh->prepare("
        SELECT 
            Customer.Primary_Email AS EMAIL,
            ...
        FROM Order_SG
            INNER JOIN Customer USING (Customer__)
            INNER JOIN Order_SG_Detail USING (Order_SG__)
            INNER JOIN Product_Ref USING (Product_Ref__)
            INNER JOIN Reduction USING (Reduction__)
        WHERE Order_SG.Server__ IN (:servers)
            ...
            AND `DATE` BETWEEN :startDate AND :endDate
            LIMIT :limit
            OFFSET :offset");

    $stmt->bindValue(':servers', implode(',', $servers));
    $stmt->bindValue(':startDate', $startDate);
    $stmt->bindValue(':endDate', $endDate);
    $stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, \PDO::PARAM_INT);

How can I retrieve the statement after the binding? A string I could echo is fine.


Solution

The in statement will not work. One method is to include the list directly in the SQL (yucky, but that is the approach). Another is to use find_in_set():

WHERE find_in_set(Order_SG.Server__, :servers) > 0 AND
. . .

However, an index cannot be used for this function.



Answered By - Gordon Linoff
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing