Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/23548-newbie-question/
Share on other sites

Hi Amy,

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

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

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/23548-newbie-question/#findComment-106914
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.