Author Topic: [SOLVED] simple mysql statement giving error.. can't figure it out.  (Read 1144 times)

0 Members and 1 Guest are viewing this topic.

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Okay, I'm quite tired so I may just be being really dim witted here, but.. what's wrong with this mysql statement?
Code: [Select]
$sqlsel = "SELECT * FROM times WHERE day = '".$day."' && group = '".$group."' && start = '".$start."'";
I've been staring at it for some time and just can't see the problem. Maybe I just need some sleep...
Whenever I try and run it it gives me this error message:
Code: [Select]
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 'group = 'Pre-School' && start = '9:30'' at line 1I just can't see what's wrong...
Handmade crafts - gimp crafts
Web design - gimp production

Offline kathas

  • Enthusiast
  • Posts: 109
    • View Profile
    • Greek Job Search Engine
Re: simple mysql statement giving error.. can't figure it out.
« Reply #1 on: May 16, 2007, 06:28:27 PM »
the and operator is not && it is 'and' without the quotes of course...
Greek Job Search Engine - Seriously Greek

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: simple mysql statement giving error.. can't figure it out.
« Reply #2 on: May 16, 2007, 06:30:44 PM »
Thanks, but that's not it. You can use both '&&' or 'and'.
I did try it with and though and it didn't work. I'm just stumped.. I can't see anything wrong with the syntax
Handmade crafts - gimp crafts
Web design - gimp production

Offline boo_lolly

  • Devotee
  • Posts: 1,171
  • Gender: Male
  • dirka dirka dirka
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #3 on: May 16, 2007, 06:32:57 PM »
the and operator is not && it is 'and' without the quotes of course...

actually, i believe it can be done either way. anyway, try this:
Code: [Select]
$sqlsel = "
        SELECT
                *
        FROM
                times
        WHERE
                day = '".$day."',
                group = '".$group."',
                start = '".$start."'
";

Offline john010117

  • Enthusiast
  • Posts: 493
  • Gender: Male
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #4 on: May 16, 2007, 06:34:49 PM »
Try this:
Code: [Select]
$sqlsel = "SELECT * FROM times WHERE day = '$day' AND group = '$group' AND start = '$start'";

I'm just throwing ideas around...
If I test every single piece of code I post in this forum, I'd still be at post #3. So no, I do not test most of my posted code.

Google is your friend. Use it. Love it.
Remember to use the [code][/code] tags when posting code!
Mark topic "SOLVED" (link found on the top left-hand side of the page only for topic starters) when it's solved!
Practical PHP Programming - One of the best free PHP online books out there. Yes, I really do freak out when I see HTML table layouts. CSS ftw!

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: simple mysql statement giving error.. can't figure it out.
« Reply #5 on: May 16, 2007, 06:37:28 PM »
nope, no good....
Handmade crafts - gimp crafts
Web design - gimp production

Offline kathas

  • Enthusiast
  • Posts: 109
    • View Profile
    • Greek Job Search Engine
Re: simple mysql statement giving error.. can't figure it out.
« Reply #6 on: May 16, 2007, 06:40:21 PM »
try echoing your query to see if there is something wrong with it...

like a single quote in $day or $group or $start
Greek Job Search Engine - Seriously Greek

Offline john010117

  • Enthusiast
  • Posts: 493
  • Gender: Male
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #7 on: May 16, 2007, 06:42:11 PM »
Backticks sometimes solved my problems.

Code: [Select]
$sqlsel = "SELECT * FROM `times` WHERE day = '$day' AND group = '$group' AND start = '$start'";

Make sure you've defined every variable that's in the query.
If I test every single piece of code I post in this forum, I'd still be at post #3. So no, I do not test most of my posted code.

Google is your friend. Use it. Love it.
Remember to use the [code][/code] tags when posting code!
Mark topic "SOLVED" (link found on the top left-hand side of the page only for topic starters) when it's solved!
Practical PHP Programming - One of the best free PHP online books out there. Yes, I really do freak out when I see HTML table layouts. CSS ftw!

Offline boo_lolly

  • Devotee
  • Posts: 1,171
  • Gender: Male
  • dirka dirka dirka
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #8 on: May 16, 2007, 06:42:19 PM »
hmm, i just noticed something. you have 'Pre-School' in your where argument. mysql may not like the '-' in there. try doing this:
Code: [Select]
$sqlsel = "
        SELECT
                *
        FROM
                times
        WHERE
                day = '". $day ."'
        AND
                group = '". str_replace('-', '\-', $group) ."'
        AND
                start = '". $start ."'
";
« Last Edit: May 16, 2007, 06:47:13 PM by boo_lolly »

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: simple mysql statement giving error.. can't figure it out.
« Reply #9 on: May 16, 2007, 06:48:59 PM »
I've already thought of that.. I changed it to something else to test it.. I've also done the same with '9:30' as well, just in case it wasn't likng the ':'
Handmade crafts - gimp crafts
Web design - gimp production

Offline boo_lolly

  • Devotee
  • Posts: 1,171
  • Gender: Male
  • dirka dirka dirka
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #10 on: May 16, 2007, 06:52:41 PM »
I've already thought of that.. I changed it to something else to test it.. I've also done the same with '9:30' as well, just in case it wasn't likng the ':'

well... what happened?? same error?

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: simple mysql statement giving error.. can't figure it out.
« Reply #11 on: May 16, 2007, 06:56:07 PM »
yeah, same thing.. I've just narrowed it down to the:
Code: [Select]
&& group = '".$group."'If I remove the group it works fine, but obviously it doesn't find what I want from the database (I just don't get errors..)
Handmade crafts - gimp crafts
Web design - gimp production

Offline DragenTopic starter

  • Devotee
  • Posts: 1,153
  • Gender: Male
  • Life's Peachy...
    • View Profile
    • Gimp Productions
Re: simple mysql statement giving error.. can't figure it out.
« Reply #12 on: May 16, 2007, 07:15:36 PM »
Okay I've sorted the problem... for some reason it didn't like my mysql cell being called group.
I changed the name to sesgroup and it's working fine! perhaps group is a reserved name for something?
I'd be interested to know.

Thanks for all the help!
Handmade crafts - gimp crafts
Web design - gimp production

Offline boo_lolly

  • Devotee
  • Posts: 1,171
  • Gender: Male
  • dirka dirka dirka
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #13 on: May 16, 2007, 07:22:20 PM »
Okay I've sorted the problem... for some reason it didn't like my mysql cell being called group.
I changed the name to sesgroup and it's working fine! perhaps group is a reserved name for something?
I'd be interested to know.

Thanks for all the help!

of course! i can't believe i didn't see it before! GROUP is a mysql argument.
http://www.tizag.com/mysqlTutorial/mysqlgroupby.php

sorry i didn't recognise that sooner. glad you figured it out bro! don't forget to mark this topic as 'solved'

Offline kenrbnsn

  • Guru
  • Freak!
  • *
  • Posts: 9,708
  • Gender: Male
    • View Profile
Re: simple mysql statement giving error.. can't figure it out.
« Reply #14 on: May 16, 2007, 07:24:46 PM »
The word "group" is a reserved word in MySQL. If you want to use a reserved word in another context in MySQL you need to surround the word with backticks -- "`" -- as in `group`.

Ken