Reply to Re: Resource ID

Your name:

Reply:


Posted by Lars Eighner on 10/19/07 11:48

In our last episode, <N70Si.1786$CN4.850@news-server.bigpond.net.au>, the
lovely and talented Kye broadcast on comp.lang.php:

>> As far as I know, that is the 71st resource you create in your script.
>> There are functions for getting the type of the resource if you don't
>> know it. A resource is a PHP-special data type and is used for files,
>> database connections and a lot more. I don't think you can get much more
>> details than the resource type.

> So is there any way to know why :

><td class="pageHeading">
> <? if ( isset($_POST[dropdown]) ) {
> $heading = mysql_query("SELECT links_categories.category,
> links_categories.cat_id FROM links_categories WHERE $_POST[dropdown] =
> links_categories.cat_id") or die('Error, Heading select failed');
> } else {
> $heading = "Markets, Fairs, Fetes and Field Days";
> }
> echo $heading; ?></td>

> is returning a Resource ID # 71 or occasionally # 73???

That's right. $heading is a Resource ID. What number it is depends upon
the particular instance of your query within your script. It is merely a
handle on the return of your query. You must fetch your result according to
the type of result you expect. See the mysql_fetch_* functions in the
manual. If you expect a row use mysql_fetch_row($heading), if you expect a
hash use mysql_fetch_assoc($heading) and so forth. Because resources are
pretty much meaningless outside of particular query instances, they are
usually named something like $result, for example:


<? if ( isset($_POST[dropdown]) ) {
$result = mysql_query("SELECT links_categories.category,
links_categories.cat_id FROM links_categories WHERE $_POST[dropdown] =
links_categories.cat_id") or die('Error, Heading select failed');
$heading = mysql_fetch_row($result);
$heading = my_function_to_make_heading_pretty($heading);
} else {
$heading = "Markets, Fairs, Fetes and Field Days";
}
echo $heading; ?>

If you experiment with the mysql client on the command line and try using it
in contexts in which you pipe results to other programs you will soon
understand that mysql results can be interpreted in different ways which is
why PHP gives you a handle on results so you can choose the appropriate
interpretation.

Also, because line breaks, quoting, escaping, and so forth in the query
string can be messy, it is typical practice to compose the query string
outside the mysql_query.

<? if ( isset($_POST[dropdown]) ) {
$query = 'query string ' . "carnage" . ' and ugliness';
$result = mysql_query($query) or die('Error, Heading select failed');
$heading = mysql_fetch_row($result);
$heading = my_function_to_make_heading_pretty($heading);
} else {
$heading = "Markets, Fairs, Fetes and Field Days";
}
echo $heading; ?>

Really, there are many examples with the mysql functions in the manual. It
would pay you to study them.

--
Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 459 days to go.
What do you do when you're debranded?

[Back to original 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

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