|
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]
|