You are here: Re: Variable in select row « PHP Programming Language « IT news, forums, messages
Re: Variable in select row

Posted by Pedro Graca on 10/08/06 20:17

Garry Jones wrote:
> A table (table1) with the following fields
>
> index, field2, town, townnum
>
> Records of
> 1 abc dallas 5
> 2 def denver 3
> 3 ghh atlanta 17
> 4 gfe gateshead 24
>
> The user knows the townnumber (5 or 8 or 17 or 24). The user does not know
> the index, but townnum will always have a unique value.
>
> He keys in his number.

Let's say it's in a POST form. So his number will be $_POST['townnum']

> I then want to look for the value of field 2 in the record where the user's
> townnumber was found.

// ...
$sql = "select field2 from table1 where townnum={$_POST['townnum']}";
$resource = mysql_query($sql);
if (!$resource) {
// something wrong with the query.
// for simplicity sake we just exit with a message
exit('Error in query: ' . mysql_error());
}

> I then want to assign a variable to the value of the field2 in that record
> If he types in 3 my code should assign the value of def to $var2

if (mysql_num_rows($resource) == 1) {
$var2 = mysql_result($resource, 0);
} else {
// Either $_POST['townnum'] does not exist, or it is not unique
// anyway, for simplicity sake, we exit with a message
exit('Invalid townnum. Go back and try again.');
}
// no more need for the resource
mysql_free_result($resource);
// ...
// $var2 is now the corresponding value from table1


--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация