user1319951
user1319951

Reputation: 505

Concatinating several values into one row works in MySQL console but not in PHP

When I try to run the following code in the MySQL console it gives me a valid output that is correct:

SELECT * , GROUP_CONCAT( phonenr
SEPARATOR  ', ' ) 
FROM employee AS e
JOIN phonenr AS p ON p.ssn = e.ssn GROUP BY e.ssn

When I run the same code in PHP:

$result = mysql_query(
'SELECT * , GROUP_CONCAT(phonenr
SEPARATOR  ', ' ) 
FROM employee AS e JOIN phonenr AS p ON p.ssn = e.ssn GROUP BY e.ssn');

I get the following error when I launch the website:

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\AppServ\www\test.php on line 17 Could not query:

Have no clue why PHP rejects this.

Upvotes: 0

Views: 57

Answers (1)

John Conde
John Conde

Reputation: 219894

It's a quote issue. You're wrapping your query in single quotes which conflicts with the single quotes you use in the GROUP_CONCAT function.

$result = mysql_query(
"SELECT * , GROUP_CONCAT(phonenr
SEPARATOR  ', ' ) 
FROM employee AS e JOIN phonenr AS p ON p.ssn = e.ssn GROUP BY e.ssn");

Upvotes: 3

Related Questions