Home > database >  Concat statement in mysql with PHP causes mysqli_query to return false
Concat statement in mysql with PHP causes mysqli_query to return false

Time:06-01

I have the following query:

$sqlquery="SELECT CONCAT_WS(".",php_version_major, php_version_minor, php_version_build) AS phpversion, COUNT(id) AS COUNT, YEAR(createdate) AS YEAR, MONTH(createdate) AS MONTH FROM logs_checklist_interface GROUP BY phpversion, YEAR(createdate), MONTH(createdate) ORDER BY createdate ASC";

$rsData=mysqli_query($dblink,$sqlquery);

$rsData returns FALSE with this query. The cause seems to be the concat_ws statement. If I remove that it works fine. I've tried the other CONCAT function and it's the same result.

What is wrong with such a simple statement that causes mysqli_query() to get upset ?

CodePudding user response:

If you do this:

$myString = "abc" . "xyz";

You get a string abcxyz, not abc"."xyz

I suppose that you think that " means something different to PHP in the cases where you intended it to be used as a string delimiter in an SQL expression.

$sqlquery="SELECT CONCAT_WS(".",php_version_major,
          ^ php quote       ^ ^ sql string delimiters?

But unfortunately, that's not true. PHP sees both instances of " as PHP quotes. Therefore this is two PHP strings, using the PHP string concatenation operator ..

So the resulting SQL query is:

SELECT CONCAT_WS(,php_version_major, ...

This has a comma directly after the parenthesis, so it's nonsense syntax.

  • Related