Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It’s FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Php Query Error.

This is a discussion on Php Query Error., within the PHP section. This forum and the thread "Php Query Error." are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Php Query Error., I can't see a problem with this.. can you?
Blake121
post Feb 14 2008, 10:06 AM
Post #1


Fresh Squeezed
**

Group: Members
Posts: 12
Joined: 14-February 08
From: Scotland
Member No.: 88



Hey guys. This isn't so much a problem as a question..

I was playing about with comment posting and I had this (so I though) simple code in my script.

CODE
   $sql = "INSERT INTO comments (user,from,comment,date) VALUES ('$id','$userID','$comment',NOW())";



I had to change it to

CODE
   $sql = "INSERT INTO `theloop`.`comments` (`user`, `from`, `comment`, `date`) VALUES ('$id', '$userID', '$comment', CURRENT_TIMESTAMP);";


I don't see why the first one didn't work and the second one did.

They're the same apart from having theloop.comments instead of just comments and CURRENT_TIMESTAMP instead of NOW().

Also when I tried putting CURRENT_TIMESTAMP in place of NOW it didn't work. Either did adding the DB name infront of the table.

Can anyone spread some light on the situation?


--------------------
Go to the top of the page
 
+Quote Post
paintingtheweb
post Feb 14 2008, 02:42 PM
Post #2


Squeezing
***

Group: Members
Posts: 70
Joined: 14-February 08
From: Las Vegas, NV
Member No.: 122



are you purposely inserting IDs as strings? And what does your connection string look like?

QUOTE (Blake121 @ Feb 14 2008, 07:06 AM) *
Hey guys. This isn't so much a problem as a question..

I was playing about with comment posting and I had this (so I though) simple code in my script.

CODE
   $sql = "INSERT INTO comments (user,from,comment,date) VALUES ('$id','$userID','$comment',NOW())";



I had to change it to

CODE
   $sql = "INSERT INTO `theloop`.`comments` (`user`, `from`, `comment`, `date`) VALUES ('$id', '$userID', '$comment', CURRENT_TIMESTAMP);";


I don't see why the first one didn't work and the second one did.

They're the same apart from having theloop.comments instead of just comments and CURRENT_TIMESTAMP instead of NOW().

Also when I tried putting CURRENT_TIMESTAMP in place of NOW it didn't work. Either did adding the DB name infront of the table.

Can anyone spread some light on the situation?


--------------------


"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." Martin Golding
Go to the top of the page
 
+Quote Post
rewake
post Feb 14 2008, 05:33 PM
Post #3


Rapid Squeezer
****

Group: Mentor
Posts: 223
Joined: 14-February 08
From: NY, USA
Member No.: 127



Hey guys,

I have a feeling it has to do with your connection string, as Blake mentioned, but I'm not sure without seeing all the code. There's no reason that I know of that the MySql NOW() function would not work.

I have a feeling it's because of a missing mysql_select_db() call before the mysql_query().

Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
Blake121
post Feb 14 2008, 05:55 PM
Post #4


Fresh Squeezed
**

Group: Members
Posts: 12
Joined: 14-February 08
From: Scotland
Member No.: 88



It's nothing to do with the connection.

I know the connection works fine, I have other query's that will run.

It seems to fix it's self when you add those slanty commas around each element. Which makes no sense to me.


--------------------
Go to the top of the page
 
+Quote Post
rewake
post Feb 14 2008, 07:29 PM
Post #5


Rapid Squeezer
****

Group: Mentor
Posts: 223
Joined: 14-February 08
From: NY, USA
Member No.: 127



OH...duh!

Reserved words. MySql has reserved words you cannot use unless quoted...

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

While you're "ok" with an unquoted "date" (i.e.: select date from logs), you should make it a practice to never use reserved words as colum names. You should also make it a practice to quote all database, table and column names when you create your queries as well. (i.e.: SELECT `log_date` FROM `logs`)

Take care,
Rich


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
Blake121
post Feb 15 2008, 09:41 AM
Post #6


Fresh Squeezed
**

Group: Members
Posts: 12
Joined: 14-February 08
From: Scotland
Member No.: 88



Ahh... I'm such an idiot. I didn't even think or reserved words.

Cheers mate!


--------------------
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   4 Sleaklight 202 15th February 2008 - 03:25 PM
Last post by: Marc
No New Posts   9 PoetAlley 296 17th February 2008 - 06:27 PM
Last post by: PoetAlley
No New Posts   3 mcdanielnc89 234 23rd February 2008 - 08:14 AM
Last post by: Marc
No new   14 JustinStudios 274 17th March 2008 - 12:04 AM
Last post by: Monie
No new   18 Mark 222 6th March 2008 - 12:40 PM
Last post by: delusion