Jump to content


Photo

Newbie question


  • Please log in to reply
4 replies to this topic

#1 nubble

nubble
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationMiddle of nowhere, ME

Posted 10 October 2006 - 03:05 PM

Can't figure out the darn syntax on this... maybe I'm trying to do something that's not possible?  I have a field for snakeID, which is how I identify one critter from another.  I want the page to display just records for that snake on the page, in this case the snakeID is BM05J10.  I can get it to work when I format the query like this:

$sql = "select `snakeID`, `date`, `weight` from `weights` where `snakeID` like 'BM05J10' ORDER BY date DESC";

So what I'd like to do is do is be able to define the snakeID I'm working on in one variable at the top of the page, so I don't have to re-write the code for every critter.

<?php $critter="BM05J10"; ?>
<html>
<head>

When I try to put these two concepts together, I get a syntax error.  I know the problem is in this line of code, but can't figure out what's wrong with it.

$sql = "select `snakeID`, `date`, `weight` from `weights` where `snakeID` like '".$critter."' ORDER BY date DESC";

When I use this, I get no records at all.

I'm sure its something dumb - newbie stuff :(

-Amy

#2 matte

matte
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 10 October 2006 - 03:16 PM

what is the syntax error you get?

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 03:47 PM

Hi Amy,

Don't worry about escaping simple variables in a double quoted string... Use this:

$critter = "BM05J10";
$sql = "SELECT `snakeID`, `date`, `weight` FROM `weights` WHERE `snakeID` LIKE '$critter' ORDER BY date DESC";

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 nubble

nubble
  • Members
  • PipPip
  • Member
  • 19 posts
  • LocationMiddle of nowhere, ME

Posted 10 October 2006 - 04:02 PM

Huggie - You rule so much.... so friggin much....  ;D

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 10 October 2006 - 04:03 PM

Glad to have been of assistance  ;)

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users