You are here: Re: fetch_array() « PHP Programming Language « IT news, forums, messages
Re: fetch_array()

Posted by Kim Andrι Akerψ on 12/23/06 06:50

Kimmo Laine wrote:

> "Geoff Berrow" <blthecat@ckdog.co.uk> wrote in message
> news:0g9mo2597of364e5tqafja4e48gr1uvg5s@4ax.com... >Message-ID:
> <1166741903.577897.67580@80g2000cwy.googlegroups.com> from
> > Jerim79 contained the following:
> >
> > > $random=rand(1,5);
> > > $query="SELECT Message FROM random_quote WHERE MessageID=$random";
> > > $message = mysql_query($query) or die(mysql_error());
> > > echo $message;
> > >
> > > But it only returns "Resource #5." I had someone mention
> > > mysql_fetch_array, but that seems like an unecessary way to go
> > > about it. To me, logically the code above should work.
> >
> > No, it returns a resource, as you have found out. The output you
> > require may just be one thing, but a query can return many rows and
> > columns of data. It needs further processing before you can do
> > anything useful with it. You might only be interested in the
> > number of rows where you could use mysql_num_rows()
> >
> > But in your case, since you know that MessageID is unique you know
> > it will only return one row.
> >
> > $random=rand(1,5);
> > $query="SELECT Message FROM random_quote WHERE MessageID=$random";
> > $result = mysql_query($query) or die(mysql_error());
> > $row= mysql_fetch_array($result);
> > echo $row['Message'];
>
> Better use mysql_fetch_assoc or pass the parameter MYSQL_ASSOC than
> use mysql_fetch_array as is, there's no need for the indexed elements.

Why go through all the hoops of using arrays when you only need the one
element?

$random=rand(1,5);
$query="SELECT Message FROM random_quote WHERE MessageID=$random";
$result = mysql_query($query) or die(mysql_error());
echo mysql_result($result, 0);

--
Kim AndrΓ© AkerΓΈ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)

 

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

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация