# MySQL Query Syntax problem

**URL:** https://boards.straightdope.com/t/mysql-query-syntax-problem/334896
**Category:** Factual Questions
**Created:** [December 9, 2005, 4:55pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896 "2005-12-09T16:55:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Fromage\_A\_Trois](https://avatars.discourse-cdn.com/v4/letter/f/a88e57/32.png) [@Fromage\_A\_Trois](https://boards.straightdope.com/u/Fromage_A_Trois)
#### Post date: [December 9, 2005, 4:55pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/1 "2005-12-09T16:55:18Z")

</div>

In the past, I have had MySQL installed on my PC, made it a server and hosted internal websites on it. I have run queries on the MySQL database, both manually and using a PHP form.

However, I now don’t have that PC, and want to host something for the world to see. I’ve got an account with a host that provides MySQL databases, and I’ve started setting up my site. However, the query syntax I’ve used in the past doesn’t seem to work. Previously, I’ve entered things like:

SELECT (first) AS first, (last) AS last FROM names

However, this remote server tells me there is a problem with my syntax. It doesn’t seem to like me using AS, or the commas, and it certainly doesn’t like the brackets. By entering:

SELECT ‘first’ ‘last’ FROM names

I managed to get something, but not the data I was after. I know this isn’t the forum for detailed MySQL questions, but if someone could just tell me if I have a different type of MySQL that I should look for manuals on, I’ll go and do it. Apparently I’m on MySQL 4.0.26, which according to all the online info I can find, should use the () syntax in my first example.

Does anyone know what I’m up against?

Thanks!

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [December 9, 2005, 5:06pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/2 "2005-12-09T17:06:16Z")

</div>

I’m no MySQL expert but I think “first” and “last” are reserved words. Like “first record” and “last record”.

So you might want to try SELECT (first) AS FirstName, (last) AS LastName FROM names, and/or change the names of your fields.

---

<div class="post-metadata">

### Author: ![Fromage\_A\_Trois](https://avatars.discourse-cdn.com/v4/letter/f/a88e57/32.png) [@Fromage\_A\_Trois](https://boards.straightdope.com/u/Fromage_A_Trois)
#### Post date: [December 9, 2005, 5:59pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/3 "2005-12-09T17:59:59Z")

</div>

My fault for not actually copying and pasting the actual query I’ve run - that was just an example of the syntax I’ve used, not the actual query. I have in fact changed the reference names, to see if it helped, but it didn’t. This is from my script:

insert into images (filename, title, desc, date, downloads, category, disc) values (’$filename’, ‘$title’, ‘$desc’, now(), 0, ‘$category’, ‘$disc’)

The error returned complains about the first (. I just can’t see the problem. Grrrrrr!

---

<div class="post-metadata">

### Author: ![ZipperJJ](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/zipperjj/32/211_2.png) [@ZipperJJ](https://boards.straightdope.com/u/ZipperJJ)
#### Post date: [December 9, 2005, 6:04pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/4 "2005-12-09T18:04:25Z")

</div>

Hmmm I don’t see the problem either, but “date” is also a reserved word…try changing that?

---

<div class="post-metadata">

### Author: ![Nanoda](https://avatars.discourse-cdn.com/v4/letter/n/ce73a5/32.png) [@Nanoda](https://boards.straightdope.com/u/Nanoda)
#### Post date: [December 9, 2005, 6:47pm UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/5 "2005-12-09T18:47:36Z")

</div>

Problems with reserved words in SQL can usually be solved by double-quoting the offending parameter. Give that a shot.

---

<div class="post-metadata">

### Author: ![Crowbar\_of\_Irony\_3](https://avatars.discourse-cdn.com/v4/letter/c/f08c70/32.png) [@Crowbar\_of\_Irony\_3](https://boards.straightdope.com/u/Crowbar_of_Irony_3)
#### Post date: [December 10, 2005, 4:33am UTC](https://boards.straightdope.com/t/mysql-query-syntax-problem/334896/6 "2005-12-10T04:33:47Z")

</div>

I see that you are using '$" before each variable, assume that you are using PHP. Here’s a PHP-based SQL statement which works for me:

```php

 // Create the SQL statement to add the user to the database.
$sql = "INSERT INTO user (username, userpassword, useremail, usergender, useraddress)";
$sql .= " VALUES ( \"$userName\", password(\"$userPassword\"), \"$userEmail\", $userGender, \"$userAddress\")";

```

If I remember correct, you might have to escape the single quote too…
