Selecting a row by value.. getting errors..

Selecting a row by value.. getting errors..

am 09.12.2006 02:07:35 von Mwahahahahaaahaa

Hello all.. here is a snippet of some code that I'm going to try and
use. My DBASE is as follows

Table name = "2006"
The columns are: month | start | days | etc....
Now I want to be able to log into the dbase/table and retrieve
information on the row based on the month. I login to the table fine,
but get an error on the QUERY. "Check the manual that corresponds to
your MySQL server version for the right syntax to use near '2006 WHERE
month = 'dec'' at line 1". I've tried it different ways.. originally
with variables, and then tried it as a solid string to see if I was
making a mistake. If anyone could point me in the right direction I
would GREATLY appreciate it!!!

//P.S. the variable $Table = "2006"
// and $selField = "dec"

mysql_connect("localhost", $User, $Pass) or die(mysql_error());
mysql_select_db($dBase) or die(mysql_error());


//$queryString = "Select * FROM '" . $Table . "' WHERE 'month' LIKE " .
$selField;
$queryString = "SELECT * FROM 2006 WHERE month = 'dec' ";
$result = mysql_query($queryString) or die(mysql_error());

Re: Selecting a row by value.. getting errors..

am 09.12.2006 05:52:13 von Rik

Mwahahahahaaahaa wrote:
> //$queryString = "Select * FROM '" . $Table . "' WHERE 'month' LIKE "
> . $selField;
> $queryString = "SELECT * FROM 2006 WHERE month = 'dec' ";
> $result = mysql_query($queryString) or die(mysql_error());


You seem to be headed towards the problem with your commented out query,
unfortunately the wrong kind of quoting:
$queryString = "SELECT * FROM `{$Table}` WHERE `month` LIKE '{$selField}'";
IMPORTANT:
- always use backticks `` (NOT '') around field & tablenames.
- always quote strings.

Normally it's not strictly necessary to backtick table & fieldnames, but
it's good practise and avoids problems like this.
--
Rik Wasmus

Re: Selecting a row by value.. getting errors..

am 09.12.2006 06:40:57 von Mwahahahahaaahaa

Thanks Rik! That seemed to have done the trick... What exactly do the {
} do? Is there a good online reference you know about that I could
learn some more? The few sites I've found are very vague on how
variables work.. like strings integers, arrays etc.. I'm used to C++ so
if I could find a comprehensive site that'd be great. Thanks again for
your help Rik, you're a life saver!

Re: Selecting a row by value.. getting errors..

am 09.12.2006 10:05:45 von Peter

> Thanks Rik! That seemed to have done the trick... What exactly do the {
> } do? Is there a good online reference you know about that I could
> learn some more? The few sites I've found are very vague on how
> variables work.. like strings integers, arrays etc.. I'm used to C++ so
> if I could find a comprehensive site that'd be great. Thanks again for
> your help Rik, you're a life saver!

the {} seperate the variable from the string which can be handy when the
variable does not have a spase after itfor example the following would cause
errors:-

$date = '9';
echo "Today is the $dateth";
?>

The script will be trying to find a variable called $dateth which does not
exist but if you do:-

$date = '9';
echo "Today is the {$date}th";
?>

In this case php now knows that $date is the variable