# Why won't this php code work??

**URL:** https://boards.straightdope.com/t/why-wont-this-php-code-work/258373
**Category:** Factual Questions
**Created:** [August 4, 2004, 11:21pm UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373 "2004-08-04T23:21:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)
#### Post date: [August 4, 2004, 11:21pm UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373/1 "2004-08-04T23:21:08Z")

</div>

$querymem\_garprating = $db-\>query(“SELECT voter, author, Avg(rating) AS garprating FROM $table\_ratings GROUP BY voter, author HAVING (voter=$xmbuser)”);

I get a syntax error on this…

I also tried this:

$querymem\_garprating = $db-\>query(“SELECT voter, author, Avg(rating) WHERE voter = ‘$xmbuser’ AS garprating FROM $table\_ratings GROUP BY voter, author”);

That didn’t work either.

Any php guru’s help me out on this one?

---

<div class="post-metadata">

### Author: ![ready29003](https://avatars.discourse-cdn.com/v4/letter/r/71e660/32.png) [@ready29003](https://boards.straightdope.com/u/ready29003)
#### Post date: [August 4, 2004, 11:22pm UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373/2 "2004-08-04T23:22:33Z")

</div>

> [@ready29003](#):
>
> $querymem\_garprating = $db-\>query(“SELECT voter, author, Avg(rating) AS garprating FROM $table\_ratings GROUP BY voter, author HAVING (voter=$xmbuser)”);

I get unknown column for the above

> [@](#):
>
> $querymem\_garprating = $db-\>query(“SELECT voter, author, Avg(rating) WHERE voter = ‘$xmbuser’ AS garprating FROM $table\_ratings GROUP BY voter, author”);
> 
> That didn’t work either.
> 
> Any php guru’s help me out on this one?

I get:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE voter = ‘Matt’ AS garprating FROM xmb\_ratings GROUP BY voter

on the second

---

<div class="post-metadata">

### Author: ![Daver914](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daver914](https://boards.straightdope.com/u/Daver914)
#### Post date: [August 5, 2004, 1:32am UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373/3 "2004-08-05T01:32:11Z")

</div>

Try the first one again, but move the GROUP BY clause to the end. Typically it goes SELECT x FROM y WHERE z HAVING w GROUP BY a ORDER BY b.

---

<div class="post-metadata">

### Author: ![Daver914](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daver914](https://boards.straightdope.com/u/Daver914)
#### Post date: [August 5, 2004, 1:36am UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373/4 "2004-08-05T01:36:17Z")

</div>

> [@Daver914](#):
>
> Try the first one again, but move the GROUP BY clause to the end. Typically it goes SELECT x FROM y WHERE z HAVING w GROUP BY a ORDER BY b.

Scratch that, the order doesn’t matter. Not in MySQL anyway.

---

<div class="post-metadata">

### Author: ![Daver914](https://avatars.discourse-cdn.com/v4/letter/d/4da419/32.png) [@Daver914](https://boards.straightdope.com/u/Daver914)
#### Post date: [August 5, 2004, 1:42am UTC](https://boards.straightdope.com/t/why-wont-this-php-code-work/258373/5 "2004-08-05T01:42:05Z")

</div>

> [@Daver914](#):
>
> Scratch that, the order doesn’t matter. Not in MySQL anyway.

In regard to the second statement, I get an error when I try and put an AS _xyz_ in the WHERE clause. Try taking out the _AS garprating_.
